linux shell之read

来源:互联网 发布:java打包war后如何发布 编辑:程序博客网 时间:2024/06/05 14:15

read常用几个选项
-n:read -n 2 var,将输入的字符串截取前2个字符放入到变量var中

root@ubuntu:~# read -n 2 var78root@ubuntu:~# echo ${var}78root@ubuntu:~# 

-p:read -p “input your name:” name, -p后面是提示字符串,通过输入将值存取到name变量中
注意:“”提示字符串与name变量之间有空格,不然会出现另外情况

root@ubuntu:~# read -p "input your name:" nameinput your name:zhangsanroot@ubuntu:~# echo ${name}zhangsanroot@ubuntu:~# 

-s:read -s var,将输入值存储到var中,但不在终端显示,不会显

#!/bin/bashread -p "input your DBName:" nameecho read -s -p "input your password:" passwordechoecho ${name}echo ${password}root@ubuntu:~/shell# ./no_echo.sh input your DBName:zhangsaninput your password:zhangsanabcd1234root@ubuntu:~/shell# 

-t:read -t 3 var,在3秒内输入数据到var变量中,不然退出

-d:read -d “|” 使用“|”作为输入结束标志

root@ubuntu:~# read -d "|" varhello|root@ubuntu:~# root@ubuntu:~# echo ${var}helloroot@ubuntu:~# 

一般-p、-t、-s可以结合使用

原创粉丝点击