shell编程_获取用户输入(read命令)
read命令,read [options] 变量1 变量2...
read从键盘、文件、管道获取值。
使用read命令从文件中获取数据
#! /bin/bash
count=1
cat file | while read line
do
echo "Line: $count $line"
count=$[ $count + 1 ]
done
echo "Finished processing the file"
使用read命令一直会等待用户输入。
可以用选项 -t 指定一个计时器。参数 -t 后面的秒数就是等待用户输入的秒数。
超时后,read命令会以非0的退出状态码退出。
#! /bin/bash
if read -t 5 -p "Please enter your name: " name
then
echo "Hello $name, Welcome to my script"
else
echo
echo "Sorry, too slow!"
fi
posted on 2021-06-20 20:48 XiaoXiaoli 阅读(175) 评论(0) 收藏 举报
浙公网安备 33010602011771号