read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量
- -p:后接屏幕输出的提示语句
- -n:设定输入的字符个数,当达到指定的个数则自动退出,并将输入赋予给变量
- -t:当输入超过指定时间,则退出,并返回一个非0状态码
- -s:选项能够使read命令中输入的数据不显示在监视器上(实际上,数据是显示的,只是read命令将文本颜色设置成与背景相同的颜色),比如密码
read 后面接的参数可以一个,也可以多个
- read -p
[root@Director ~]# read -p "Please input two number: " n1 n2 #注意空格 Please input two number: 9 8 [root@Director ~]# echo $n1,$n2 9,8
AIX机器上是不支持-p的,这个时候可以用echo -n进行替代
[root@Director ~]# echo -n "Please input two number:";read n1 n2 Please input two number:1 2 [root@Director ~]# echo $n1,$n2 1,2
- read -n
[root@Director ~]# read -n3 -p "Please input a number: " var1 Please input a number: 123[root@Director ~]#
- read -t
[root@Director test]# cat demo.sh #/bin/bash if read -t 5 -p "Please input your anwser: " anwser then echo $anwser else echo -e " sorry,timeout" fi [root@Director test]# sh demo.sh Please input your anwser: sorry,timeout
- read -s
[root@Director test]# read -s -p "Pleas input your password: " p Pleas input your password: [root@Director test]# echo $p 12345
生产应用,根据端口号杀进程
lsof -i :8080 |awk '{print $2}'|while read pid do if [ "${pid}" != "PID" ];then kill -9 $pid break fi done