Shell编程-read命令

1.read
读取输入的值
语法 read[选项] 值
-p 提示语句
-n 字符个数
-t 等待时间,秒
-s 隐藏输入

2.例子:
等待3秒输入,提示语句please input your name:
#!/bin/bash

read -t 3 -p "please input your name:" name
echo $name


保存,并chmod +x read.sh

执行:./read.sh

可以看到,3秒后就退出了。

[root@VM_0_16_centos es]# ./read.sh
please input your name:
[root@VM_0_16_centos es]#

如果在3秒内输入文本:
[root@VM_0_16_centos es]# ./read.sh
please input your name:hello
hello

例子:隐藏输入的内容

#!/bin/bash

read -st 10 -p "please input your name:" name
echo $name

执行:./read.sh

输入的时候,看不到你输入了什么内容
[root@VM_0_16_centos es]# ./read.sh
please input your name:hello
[root@VM_0_16_centos es]#

ctrl+退格:退格操作


例子:-n 1  只允许输入1个字符,就打印出来了。
~
#!/bin/bash

read -t 10 -p "please input your name:" name
echo $name

read -t 30  -sp "input your age:" age
echo $age
#!/bin/bash

#read -t 10 -p "please input your name:" name
#echo $name

#read -t 30  -sp "input your age:" age
#echo $age


read -t 30 -n 1  -p "input your gender[m|f]:" gender
echo $gender


[root@VM_0_16_centos es]# ./read.sh
input your gender[m|f]:mm
[root@VM_0_16_centos es]#

第一个m是输入的,第二个是打印的

例子3:输入多个值
#!/bin/bash

read -t 10 -p "please input your name and age:" name age
echo $name $age

执行:

[root@VM_0_16_centos es]# ./read.sh
please input your name and age:hello 18
hello 18
[root@VM_0_16_centos es]#




















你可能感兴趣的:(Linux,Shell)