shell编程,获得用户输入read命令
获取输入:
read variable... 读取用户输入命令,变量可以有多个
-p 可以在read后面加上提示文字,例:read -p "please enter you name" name
例:read的基本使用
read name
echo $name
方法一:
echo -p "Please enter you name : " name
echo "You name is $name"
方法二:read后面有多个变量
read -p "please enter you name : " name age
echo "You name is $name ,You age is $age"
方法三:通过环境变量REPLY读取输入的值
read -p "enter you infomation :"
echo "You infomation are $REPLY"
输入超时:
当脚本运行到需要输入但是没有输入时,使用 -t 选项可以为read命令指定一个计时器,计时器过期后read会返回一个非零退出状态码。
例:
if read -t 5 -p "Enter you name:" name ; then
echo "You name is:" $name
else
echo "overtime"
fi
输入指定个数字符:
使用-n1参数(“1”为字符个数)可以指定输入个数,当达到指定字符个数后即退出,不用按回车键。
例:
read -n1 -p "enter y/n" answer
case $answer in
y | Y ) echo "yes" ;;
n | N) echo "no" ;;
esac
隐藏输入信息:
有时需要隐藏输入的信息比如密码,用 -s 选项可以隐藏输入的信息。
read -s -p "Enter you pass:" pass
echo "You pass is $pass"
从文件中读取:
每次调用read命令都会读取一行文本,当没有内容时返回非零状态码并退出
例:
cat demo.txt | while read line
do
echo "$line"
done
- shell编程,while循环,until循环命令
- shell编程,if-then、else,case,数值、字符串、文件类型比较检测,[ ],(()),[[ ]]
- shell编程,数学运算 expr、$[ operation ]、bc计算器
- shell检查能否免秘钥
- linux的shell,which,type,history,alias,coproc后台模式,协程,子shell
- shell脚本对 mongo 备份并导入
- shell编程,重定向错误信息和正常输出的数据,输出输入文件描述符
- shell编程,记录输出的消息,tee命令
- shell编程,信号的捕获修改和移除,trap命令,在脚本中处理信号
- shell 删除空目录