当前位置:网站首页>Shell script -read command: read data entered from the keyboard
Shell script -read command: read data entered from the keyboard
2022-07-01 08:48:00 【Little snail's way】
read yes Shell Built in commands , It is used to read data from standard input and assign values to variables . If there is no redirection , The default is to read the data entered by the user from the keyboard ; If redirected , Then you can read data from the file .
read The usage of the command is :
read [-options] [variables]
options Said options , As shown in the following table ;variables Represents the variable used to store data , There can be one , There can be more than one .options and variables It's all optional , If the variable name is not provided , Then the read data will be stored in the environment variable REPLY in .
| Options | explain |
|---|---|
| -a array | Assign the read data to the array array, From the subscript 0 Start . |
| -d delimiter | Use string delimiter Specify the position where the reading ends , Instead of a newline ( The data read does not include delimiter). |
| -e | When getting user input , Code conversion of function keys , The character corresponding to the function key will not be directly explicit . |
| -n num | Read num Characters , Instead of a whole line of characters . |
| -p prompt | Display prompt message , The tip is prompt. |
| -r | Read as is (Raw mode), Do not interpret backslash characters as escape characters . |
| -s | silent mode (Silent mode), The entered characters will not be displayed on the screen . When entering passwords and other confirmation information , It is necessary to . |
| -t seconds | Set timeout , The unit is in seconds . If the user does not complete the input within the specified time , that read Will return a non 0 The exit status of , Indicates a read failure . |
| -u fd | Use file descriptors fd As an input source , Instead of standard input , Similar to redirection . |
Code 1 Use read Command assigns values to multiple variables
#!/bin/bash
read -p "Enter some information > " name url age
echo " Website name :$name"
echo " website :$url"
echo " Age :$age"
Output :
Enter some information > jack www.baidu.com 7
Website name :jack
website :www.baidu.com
Age :7
Be careful , All values must be entered on one line , Can't break , Otherwise, only the first variable can be assigned , Subsequent variables will fail to be assigned .
This example also USES -p Options , This option will prompt the user to enter... With a piece of text .
Code 2 Read only one character
#!/bin/bash
read -n 1 -p "Enter a char > " char
printf "\n" # Line break
echo $char
Output :
Enter a char > 1
1
-n 1 Indicates that only one character is read . After running the script , As long as the user enters one character , Immediate read end , Don't wait for the user to press enter .printf "\n" Statement is used to achieve the effect of line breaking , otherwise echo The output of the will be on the same line as the user input , It's not easy to distinguish .
Code 3 Enter the password within the specified time
#!/bin/bash
if
read -t 20 -sp "Enter password in 20 seconds(once) > " pass1 && printf "\n" && # Enter the password for the first time
read -t 20 -sp "Enter password in 20 seconds(again)> " pass2 && printf "\n" && # Enter the password for the second time
[ $pass1 == $pass2 ] # Determine whether the two passwords are equal
then
echo "Valid password"
else
echo "Invalid password"
fi
In this code , We use && Combined multiple commands , These commands will be executed in turn , And as a whole if The judgment condition of a sentence , As long as one of the commands fails ( Exit status is not 0 value ), The whole judgment condition fails , There is no need to execute subsequent commands .
Output :
If the two passwords are the same , The running result is :
Enter password in 20 seconds(once) >
Enter password in 20 seconds(again)>
Valid password
If the two passwords are different , The running result is :
Enter password in 20 seconds(once) >
Enter password in 20 seconds(again)>
Invalid password
If the first input times out , The running result is :
Enter password in 20 seconds(once) > Invalid password
If the second input times out , The running result is :
Enter password in 20 seconds(once) >
Enter password in 20 seconds(again)> Invalid password
边栏推荐
- Model and view of QT
- Vscode customize the color of each area
- TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
- 毕业季,我想对你说
- Principle and application of single chip microcomputer - off chip development
- C language student information management system
- Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
- MATLAB【函数和图像】
- 深度学习训练样本扩增同时修改标签名称
- 固定资产管理系统让企业动态掌握资产情况
猜你喜欢

15Mo3 German standard steel plate 15Mo3 chemical composition 15Mo3 mechanical property analysis of Wuyang Steel Works

【面试必刷101】链表

《单片机原理及应用》-片外拓展

factory type_ Id:: create process resolution

分享2022上半年我读过的7本书

TV size and viewing distance

1.jetson与摄像头的对接

19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition

Nacos - gestion de la configuration

What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
随机推荐
长安链同步节点配置与启动
[MFC development (16)] tree control
Li Kou 1358 -- number of substrings containing all three characters (double pointer)
What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR
截图小妙招
I would like to know the process of stock registration and account opening by mobile phone? In addition, is it safe to open a mobile account?
中小企业固定资产管理办法哪种好?
Shell脚本-变量的定义、赋值和删除
内存大小端
Shell脚本-case in 和正则表达式
《单片机原理与应用》——并行IO口原理
Shell脚本-read命令:读取从键盘输入的数据
性能提升2-3倍!百度智能云第二代昆仑芯服务器上线
个人装修笔记
Configuration and startup of Chang'an chain synchronization node
TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
Share 7 books I read in the first half of 2022
嵌入式工程师面试题3-硬件
《单片机原理及应用》-片外拓展
【C】 Summary of wrong questions in winter vacation