当前位置:网站首页>Shell脚本-read命令:读取从键盘输入的数据
Shell脚本-read命令:读取从键盘输入的数据
2022-07-01 08:36:00 【小蜗牛的路】
read 是 Shell 内置命令,用来从标准输入中读取数据并赋值给变量。如果没有进行重定向,默认就是从键盘读取用户输入的数据;如果进行了重定向,那么可以从文件中读取数据。
read 命令的用法为:
read [-options] [variables]
options表示选项,如下表所示;variables表示用来存储数据的变量,可以有一个,也可以有多个。options和variables都是可选的,如果没有提供变量名,那么读取的数据将存放到环境变量 REPLY 中。
| 选项 | 说明 |
|---|---|
| -a array | 把读取的数据赋值给数组 array,从下标 0 开始。 |
| -d delimiter | 用字符串 delimiter 指定读取结束的位置,而不是一个换行符(读取到的数据不包括 delimiter)。 |
| -e | 在获取用户输入的时候,对功能键进行编码转换,不会直接显式功能键对应的字符。 |
| -n num | 读取 num 个字符,而不是整行字符。 |
| -p prompt | 显示提示信息,提示内容为 prompt。 |
| -r | 原样读取(Raw mode),不把反斜杠字符解释为转义字符。 |
| -s | 静默模式(Silent mode),不会在屏幕上显示输入的字符。当输入密码和其它确认信息的时候,这是很有必要的。 |
| -t seconds | 设置超时时间,单位为秒。如果用户没有在指定时间内输入完成,那么 read 将会返回一个非 0 的退出状态,表示读取失败。 |
| -u fd | 使用文件描述符 fd 作为输入源,而不是标准输入,类似于重定向。 |
代码1 使用 read 命令给多个变量赋值
#!/bin/bash
read -p "Enter some information > " name url age
echo "网站名字:$name"
echo "网址:$url"
echo "年龄:$age"
输出:
Enter some information > jack www.baidu.com 7
网站名字:jack
网址:www.baidu.com
年龄:7
注意,必须在一行内输入所有的值,不能换行,否则只能给第一个变量赋值,后续变量都会赋值失败。
本例还使用了-p选项,该选项会用一段文本来提示用户输入。
代码2 只读取一个字符
#!/bin/bash
read -n 1 -p "Enter a char > " char
printf "\n" #换行
echo $char
输出:
Enter a char > 1
1
-n 1表示只读取一个字符。运行脚本后,只要用户输入一个字符,立即读取结束,不用等待用户按下回车键。printf "\n"语句用来达到换行的效果,否则 echo 的输出结果会和用户输入的内容位于同一行,不容易区分。
代码3 在指定时间内输入密码
#!/bin/bash
if
read -t 20 -sp "Enter password in 20 seconds(once) > " pass1 && printf "\n" && #第一次输入密码
read -t 20 -sp "Enter password in 20 seconds(again)> " pass2 && printf "\n" && #第二次输入密码
[ $pass1 == $pass2 ] #判断两次输入的密码是否相等
then
echo "Valid password"
else
echo "Invalid password"
fi
这段代码中,我们使用&&组合了多个命令,这些命令会依次执行,并且从整体上作为 if 语句的判断条件,只要其中一个命令执行失败(退出状态为非 0 值),整个判断条件就失败了,后续的命令也就没有必要执行了。
输出:
如果两次输入密码相同,运行结果为:
Enter password in 20 seconds(once) >
Enter password in 20 seconds(again)>
Valid password
如果两次输入密码不同,运行结果为:
Enter password in 20 seconds(once) >
Enter password in 20 seconds(again)>
Invalid password
如果第一次输入超时,运行结果为:
Enter password in 20 seconds(once) > Invalid password
如果第二次输入超时,运行结果为:
Enter password in 20 seconds(once) >
Enter password in 20 seconds(again)> Invalid password
边栏推荐
- 集团公司固定资产管理的痛点和解决方案
- 19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition
- 软件工程师面试刷题网站、经验方法
- 《单片机原理及应用》—定时器、串行通信和中断系统
- 避免按钮重复点击的小工具bimianchongfu.queren()
- 【js逆向】md5加密参数破解
- Programming with C language: calculate with formula: e ≈ 1+1/1+ 1/2! …+ 1/n!, Accuracy is 10-6
- Serial port to WiFi module communication
- MySQL8.0学习记录17 -Create Table
- vscode自定义各个区域的颜色
猜你喜欢

《微机原理》——微处理器内部及外部结构

Internet of things technology is widely used to promote intelligent water automation management

Audio audiorecord create (I)

Insert mathematical formula in MD document and mathematical formula in typora

What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR

3. Detailed explanation of Modbus communication protocol

What is the material of 15CrMoR, mechanical properties and chemical analysis of 15CrMoR

It is designed with high bandwidth, which is almost processed into an open circuit?

NIO-零拷贝

19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition
随机推荐
NIO-零拷贝
Shell脚本-select in循环
TypeError: __init__() got an unexpected keyword argument ‘autocompletion‘
3、Modbus通讯协议详解
嵌入式工程师面试题3-硬件
ARM v7的体系结构A、R、M区别,分别应用在什么领域?
Screenshot tips
Redis publish subscription
Field agricultural irrigation system
Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates
Do you know how data is stored? (C integer and floating point)
【MFC开发(16)】树形控件Tree Control
Dynamic proxy
Qt的模型与视图
[MFC development (17)] advanced list control list control
电视机尺寸与观看距离
[JS reverse] MD5 encryption parameter cracking
Introduction to R language
基础:2.图像的本质
一文纵览主流 NFT 市场平台版税、服务费设计