English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Linux read command is used to read numerical 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 used with redirection, it can read a line of data from a file.
read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
Parameter Description:
1, simple reading
#!/bin/bash #Here, a new line is默认会换行 echo "Enter the website name: " #Read input from the keyboard read website echo "The website name you entered is $website" exit 0 #exit
Test results are:
Enter the website name: www.oldtoolbag.com The website name you entered is www.oldtoolbag.com
2,-p parameter, allowing a prompt to be specified directly in the read command line.
#!/bin/bash read -p "Enter the website name: " website echo "The website name you entered is $website" exit 0
Test results are:
Enter the website name:www.oldtoolbag.com The website name you entered is www.oldtoolbag.com
3,-t parameter specifies the number of seconds the read command waits for input. When the timer is up, the read command returns a non-zero exit status.
#!/bin/bash if read -t 5 -p "Enter the website name: " website then echo "The website name you entered is $website" else echo "\nSorry, you have exceeded the time limit for input." fi exit 0
The executing program does not input, waiting 5 seconds later:
Enter the website name: Sorry, you have exceeded the time limit for input
4, in addition to inputting time counting, you can also use -n Parameter settings read The command counts the input characters. When the number of input characters reaches the predetermined number, it will automatically exit and assign 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-n option, followed by a number 1, indicating that the read command will exit as soon as it receives a character. Just press a character to answer, and the read command will immediately accept the input and pass it to the variable without pressing the Enter key.
Only accepts 2 Input and exit:
#!/bin/bash read -n2 -p "Please enter two characters at will: " any echo "\nYou entered the two characters are:$any" exit 0
Execute program and enter two characters:
Please enter two characters at will: 12 The two characters you entered are:12
5,-s The option can make read The data entered in the command does not appear on the command terminal (in fact, the data is displayed, but read The command sets the text color to the same as the background color). This option is commonly used when entering a password.
#!/bin/bash read -s -p "Please enter your password: " pass echo "\nYou entered the password is $pass" exit 0
When executing the program, the password entered is not displayed:
Please enter your password: The password you entered is w3codebox
6.Read file
Each call to the read command reads a "line" of text from the file. When there are no more readable lines in the file, the read command exits with a non-zero status.
How to pass the data in the file to read? Use the cat command and pass the result directly to the while command containing the read command through a pipe.
The content of the test file test.txt is as follows:
123 456 w3codebox
Test Code:
#!/bin/bash count=1 # Assignment statement, no space cat test.txt | while read line # The output of the cat command is input to the read command, the value read to > is placed in line do echo "Line $count:$line" count=$[ $count + 1 ] # Note the space in the brackets. done echo "finish" exit 0
Execution result is:
Line 1:123 Line 2:456 Line 3:w3codebox finish
Use -e arguments, the following example input characters a Press Tab Press the key and it will output the relevant filenames (the directory exists):
$ read -e -p "Enter filename:" str Enter filename: a a.out a.py a.pyc abc.txt Enter filename: a