Shell编程-read命令

10 篇文章 0 订阅
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]#




















  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值