Linux read command
The Linux read command is used to read values from standard input.
The read internal command is used to read a single line of data from standard input. This command can be used to read keyboard input, and when redirecting is used, it can read a line of data from a file.
Syntax
read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
Parameter description:
- -a followed by a variable; this variable is treated as an array, and then assigned values. By default, spaces are used as delimiters.
- -d followed by a marker; in fact, only the first character after it is used as the end marker.
- -p followed by a prompt message, i.e., prints a prompt message before input.
- -e enables command completion when typing.
- -n followed by a number defines the length of the input text; very practical.
- -r masks \. Without this option, \ is treated as an escape character; with it, \ is just a normal character.
- -s silent mode, input characters are not displayed on the screen, e.g., entering a password during login.
- -t followed by seconds defines the waiting time for character input.
- -u followed by fd, reads from a file descriptor; this file descriptor can be newly opened by exec.
Examples
1. Simple reading
#!/bin/bash #这里默认会换行 echo "输入网站名: " #读取从键盘的输入 read website echo "你输入的网站名是 $website" exit 0 #退出
The test result is:
输入网站名: www.example.com 你输入的网站名是 www.example.com
2. The -p parameter allows a prompt to be specified directly on the read command line.
#!/bin/bash read -p "输入网站名:" website echo "你输入的网站名是 $website" exit 0
The test result is:
输入网站名:www.example.com 你输入的网站名是 www.example.com
3. The -t parameter specifies the number of seconds the read command waits for input. When the timer expires, the read command returns a non-zero exit status.
#!/bin/bash
if read -t 5 -p "输入网站名:" website
then
echo "你输入的网站名是 $website"
else
echo "\n抱歉,你输入超时了。"
fi
exit 0
Run the program without input, after waiting 5 seconds:
Enter website name:
4. In addition to input time timing, you can also use-nparameter settingreadThe command counts the entered characters. When the number of entered characters reaches a predetermined number, it automatically exits and assigns the input data to the variable.
#!/bin/bash
read -n1 -p "Do you want to continue [Y/N]?" answer
case $answer in
Y | y)
echo "fine ,continue";;
N | n)
echo "ok,good bye";;
*)
echo "error choice";;
esac
exit 0
This example uses the -n option, followed by the value 1, instructing the read command to exit as soon as it receives one character. As long as you press one character to answer, the read command immediately accepts the input and assigns it to the variable, without needing to press Enter.
Exits after receiving only 2 inputs:
#!/bin/bash read -n2 -p "请随便输入两个字符: " any echo "\n您输入的两个字符是:$any" exit 0
Run the program and enter two characters:
请随便输入两个字符: 12 您输入的两个字符是:12
5、-sThe option can makereadthe data entered in the command is not displayed on the command terminal (in fact, the data is displayed, butreadthe command sets the text color to the same color as the background). This option is commonly used when entering passwords.
#!/bin/bash read -s -p "请输入您的密码:" pass echo "\n您输入的密码是 $pass" exit 0
After running the program and entering the password, it is not displayed:
请输入您的密码: 您输入的密码是 example
6. Reading a file
Each time the read command is called, it reads "one line" of text from the file. When there are no readable lines in the file, the read command exits with a non-zero status.
How do you pass the data in a file to read? Use the cat command and pipe the output directly to a while command containing the read command.
The contents of the test file test.txt are as follows:
123 456 example
Test code:
#!/bin/bash count=1 # 赋值语句,不加空格 cat test.txt | while read line # cat 命令的输出作为read命令的输入,read读到>的值放在line中 do echo "Line $count:$line" count=$[ $count + 1 ] # 注意中括号中的空格。 done echo "finish" exit 0
The execution result is:
Line 1:123 Line 2:456 Line 3:example finish
Use-eparameter, the following example enters charactersathen pressTabthe key, and it will output the relevant file name (existing in that directory):
$ read -e -p "输入文件名:" str 输入文件名:a a.out a.py a.pyc abc.txt 输入文件名:aOther extensions