当前位置: 移动技术网 > 科技>人工智能>云计算 > shell的read方法使用介绍

shell的read方法使用介绍

2018年01月30日  | 移动技术网科技  | 我要评论

shell的read方法使用介绍,read命令-p(提示语句) -n(字符个数) -t(等待时间) -s(不回显) 和“读文件”深入学习。

1.read -p 允许指定提示语句

#!/bin/bash

read -p "input your name:" name

echo $name

exit 0

read命令也可以不指定变量,read读取 的内容会默认保存自REPLY中

 

#!/bin/bash

read -p "input your name:"

echo $REPLY

exit 0


2.read -t 指定等待超时的时间

#!/bin/bash

if read -t 10 -p "input your name: " name; then

echo "$name"

else

echo "sorry "

fi

exit 0

3.read -n 命令还可以指定输入的字符数 ,当输入的字符数达到设定的 值,自动退出,并将输入的字符赋值给变量

#!/bin/bash

read -n 1 -p "Y/N" choice; then

echo $choice

exit 0

4.read -s 输入的内容不在屏幕上显示,例如用户输入的密码

read -s -p "input your passwd:" mypasswd

echo $mypasswd

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网