本讲内容主要讲解read命令。通过学习我们已经知道如何查询Bash内置命令帮助。首先查看read命令帮助文档。
[root@promote ~]# man read
[root@promote ~]#
[root@promote ~]# info read | less
[root@promote ~]#
read 常用参数有-p 、-t、和-s。
下文代码将分别演示简单用法。
#-p 等待用户输入内容
[root@promote ~]# read -p "please input name: " name
please input name: www
#-t 设置等待时间,默认秒
[root@promote ~]# cat testreadv1.0.sh
#!/bin/bash
if read -t 5 -p "please input name: " name
then
echo "name is : $name"
else
echo "input error..."
fi
exit 0
[root@promote ~]# cat testreadv1.0.sh
#!/bin/bash
if read -t 5 -p "please input name: " name
then
echo "name is : $name"
else
echo -e "\ninput error..."
fi
exit 0
[root@promote ~]# bash testreadv1.0.sh
please input name: www
name is : www
#第二次等待5秒未输入内容
[root@promote ~]# bash testreadv1.0.sh
please input name: www
input error...
#思考问题:如何清除错误输出内容到下一行,本次操作实际输入www未按回车键
[root@promote ~]# www
#-s 不前台显示输入内容
[root@promote ~]# cat testreadv1.1.sh
#!/bin/bash
read -s -p "please input password:" passwd
echo -e "\npassword is :$passwd"
exit 0
[root@promote ~]#
[root@promote ~]# bash testreadv1.1.sh
please input password:
password is :www
[root@promote ~]#