shell read命令怎么用
发表于:2024-09-30 作者:千家信息网编辑
千家信息网最后更新 2024年09月30日,本篇内容介绍了"shell read命令怎么用"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!shel
千家信息网最后更新 2024年09月30日shell read命令怎么用
本篇内容介绍了"shell read命令怎么用"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
shell作为一门语言,自然也具有读数据的功能,read就是按行从文件(或标准输入或给定文件描述符)中读取数据的最佳选择。当使用管道、重定向方式组合命令时感觉达不到自己的需求时,不妨考虑下while read line。
1、read基本读取
1 #!/bin/bash 2 #testing the read command 3 4 echo -n "Enter you name:" #echo -n 让用户直接在后面输入 5 read name #输入的多个文本将保存在一个变量中 6 echo "Hello $name, welcome to my program." 执行:# ./read.shEnter you name: wangtaoHello wangtao, welcome to my program.
2、read -p (直接在read命令行指定提示符)
1 #!/bin/bash 2 #testing the read -p option 3 read -p "Please enter your age: " age 4 days=$[ $age * 365 ] 5 echo "That makes you over $days days old!"执行:# ./age.shPlease enter your age: 23That makes you over 8395 days old!
3、read -p (指定多个变量)
1 #!/bin/bash 2 # entering multiple variables 3 4 read -p "Enter your name:" first last 5 echo "Checking data for $last, $first"执行:# ./read1.shEnter your name: a bChecking data for b, a
4、read 命令中不指定变量,那么read命名将它收到的任何数据都放在特殊环境变量REPLY中
1 #!/bin/bash 2 # testing the REPLY environment variable 3 4 read -p "Enter a number: " 5 factorial=1 6 for (( count=1; count$REPLY; count++ )) 7 do 8 factorial=$[ $factorial * $count ] #等号两端不要有空格 9 done10 echo "The factorial of $REPLY is $factorial"执行:./read2.shEnter a number: 6The factorial of 6 is 720
5、超时, 等待输入的秒数(read -t)
1 #!/bin/bash 2 # timing the data entry 3 4 if read -t 5 -p "Please enter your name: " name #记得加-p参数, 直接在read命令行指定提示符 5 then 6 echo "Hello $name, welcome to my script" 7 else 8 echo 9 echo "Sorry, too slow!"10 fi执行:# ./read3.shPlease enter your name:Sorry, too slow!# ./read3.shPlease enter your name: wangHello wang, welcome to my script
6、read命令对输入的字符判断
1 #!/bin/bash 2 # getting just one character of input 3 4 read -n1 -p "Do you want to continue [Y/N]? " answer 5 case $answer in 6 Y | y) echo 7 echo "fine, continue on...";; 8 N | n) echo 9 echo "OK, goodbye"10 exit;;11 esac 执行:# ./read4.shDo you want to continue [Y/N]? yfine, continue on..../read4.shDo you want to continue [Y/N]? nOK, goodbye
7、隐藏方式读取(read -s)
1 #!/bin/bash 2 # hiding input data from the monitor 3 4 read -s -p "Enter your passwd: " pass #-s 参数使得read读入的字符隐藏 5 echo 6 echo "Is your passwd readlly $pass?"~ 执行:# ./read5.shEnter your passwd:Is your passwd readlly osfile@206?
8、从文本中读取
1 #!/bin/bash 2 # reading data from a file 3 4 count=1 5 cat test | while read line 6 do 7 echo "Line $count: $line" 8 count=$[ $count + 1 ] 9 done10 echo "Finished processing the file"执行:./read6.shLine 1: The quick brown dog jumps over the lazy fox.Line 2: This is a test, this is only a test.Line 3: O Romeo, Romeo! Wherefore art thou Romeo?Finished processing the file
"shell read命令怎么用"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
命令
输入
变量
数据
内容
参数
多个
字符
提示符
文件
文本
方式
更多
知识
提示
实用
特殊
学有所成
接下来
两端
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库左连接方法
学校网络安全警示教育片
县网络安全自查工作报告
网络安全英文视频下载
没有数据库能进入网站吗
hcie网络技术实验指南
网络安全主题活动思想
苹果手机 软件开发IDE
天龙八部似水流年服务器炸了
设置文件服务器
无法加入网络安全教育
自己开发小程序要租服务器
滨州管理系统软件开发
linux服务器迁移
达梦数据库创建数据文件
计算机网络技术要求数学要好吗
宿州网络安全论坛
上位机软件开发工具mfc
网络安全渗透违法吗
山东弘度互联网科技有限公司
学生网络安全重要
闵行区正规软件开发质量保障
吃鸡为什么要换服务器
网络安全 精彩一课
ssh框架数据库连接原理
数据库用户创建
拨打座机号码无法连接服务器
游戏营运平台App软件开发
国家推动建设的网络安全体系
方可梦数据库