shell中read使用

(1) 下面的语句从输入中读取n个字符并存入变量variable_name:

read -n number_of_chars variable_name
例如:
[root@host1 shell]# read -n 2 var 12[root@host1 shell]# echo $var 12 [root@host1 shell]# read -n 2 var a+[root@host1 shell]# echo $var a+

(2) 用无回显的方式读取密码:

[root@host1 shell]# read -s var
[root@host1 shell]# echo $var
hello

(3) 显示提示信息:

[root@host1 shell]# read -p "Input:" var
Input:abc
[root@host1 shell]# echo $var
abc
以无回显方式输入密码:
[root@host1 shell]# read -s -p  "Input Password:" pass
Input Password:[root@host1 shell]# echo $pass
mypassword

(4) 在特定时限内读取输入:

read -t timeout var
例如:
$ read -t 2 var
#在2秒内将键入的字符串读入变量var

(5) 用特定的定界符作为输入行的结束:

read -d delim_char var
例如:
[root@host1 shell]# read -d ":" var
hello:[root@host1 shell]# echo $var
hello

 

posted on 2017-06-06 10:45  Alan丶文  阅读(283)  评论(0编辑  收藏  举报