Linux read command
The Linux read command is used to read values from standard input.
The read shell builtin is used to read a single line of data from standard input. This command can read keyboard input, and when used with redirection, 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, which is treated as an array and then assigned values; spaces are used as the default delimiter.
- -d Followed by a delimiter; only the first character after it actually matters, serving as the end marker.
- -p Followed by a prompt message, which is printed before input.
- -e Enable command-line completion during input.
- -n Followed by a number that defines the length of the input text; very useful.
- -r Disable backslash escaping; without this option, \ acts as an escape character, while with it \ is treated as a normal character.
- -s Silent mode: characters are not shown on screen while typing, for example when entering a password at login.
- -t Followed by a number of seconds, defining how long to wait for input.
- -u Followed by an fd, reading input from that file descriptor, which may be one newly opened with exec.
Example
1. Simple reading
#!/bin/bash
#This will default to a newline
echo "输入网站名: "
#Read input from keyboard
read website
echo "你输入的网站名是 $website"
exit 0 #退出
The test result is:
Enter website name:
www.runoob.com
你输入的网站名是 www.runoob.com
2. The -p option allows you to specify a prompt directly in the read command line.
#!/bin/bash
read -p "输入网站名:" website
echo "你输入的网站名是 $website"
exit 0
The test result is:
输入网站名:www.runoob.com
你输入的网站名是 www.runoob.com
3. The -t option specifies the number of seconds the read command waits for input; when the timer expires, read returns a nonzero exit status.
#!/bin/bash
if read -t 5 -p "输入网站名:" website
then
echo "你输入的网站名是 $website"
else
echo "\n抱歉,你输入超时了。"
fi
exit 0
Run the program without input and wait 5 seconds:
Enter website name:
抱歉,你输入超时了
4. Besides timing the input, you can also use the -n option to set the read command to count input characters. When the number of input characters reaches the preset count, it exits automatically 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, telling the read command to exit as soon as it receives one character. As soon as you press a character in response, the read command immediately accepts the input and passes it to the variable, with no need to press Enter.
Exit after receiving only 2 characters:
#!/bin/bash
read -n2 -p "请随便输入两个字符: " any
echo "\n您输入的两个字符是:$any"
exit 0
Run the program and enter two characters:
请随便输入两个字符: 12
您输入的两个字符是:12
5. -s The option can make the read data entered in the command not appear on the terminal (in fact, the data is displayed; it's just that the read command sets the text color to match the background). This option is often used when entering passwords.
#!/bin/bash
read -s -p "请输入您的密码:" pass
echo "\n您输入的密码是 $pass"
exit 0
Run the program; the password is not displayed after entry:
Please enter your password:
您输入的密码是 runoob
6. Reading files
Each call to the read command reads one line of text from the file. When there are no more lines to read, the read command exits with a nonzero status.
How do you pass data from a file to read? Use the cat command and pipe the result directly to a while command that contains the read command.
The contents of the test file test.txt are as follows:
123
456
runoob
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 result of execution is:
Line 1:123
Line 2:456
Line 3:runoob
finish
Use -e parameter, the following example enters a character a and then presses the Tab key to output the relevant filenames (those that exist in the directory):
$ read -e -p "输入文件名:" str
输入文件名:a
a.out a.py a.pyc abc.txt
输入文件名:a
Linux Command Reference