Shell - read
1、命令交互
read是shell脚本用来获取交互窗口传输参数
格式: read -p “提示语” -n [字符个数] -t [等待时间s] -s value
-p:提示语
-n:待输入字符个数,一旦达到个数,不用回车就继续运行
-t:阻塞等待参数输入的时间,单位s;超时未输入,则结算等待继续运行
value:变量,用来接收输入的参数
-s:代表不回显输入的参数,常用于输入密码
例如:
//该脚本用于屏幕输入name,10s内输入则打印输入的name,超时未输入则打印timeout
#!/bin/bash
if read -p "Enter your name:" -t 10 name
then
echo "$name"
else
echo "timeout"
fi
运行结果
//10s内输入
[root@localhost home]# ./csdn.sh
Enter your name:zhangsan
zhangsan
[root@localhost home]#
[root@localhost home]#
//10s内不输入
[root@localhost home]# ./csdn.sh
Enter your name:timeout
[root@localhost home]#
2、read读文件
while read line
do
xxxxxx
done < file
for line in `cat file`
do
xxxxxx
done