English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Linux read Befehl wird verwendet, um Werte von der Standard-Eingabe zu lesen.
read internes Kommando wird verwendet, um Zeilen von der Standard-Eingabe zu lesen. Dieses Kommando kann verwendet werden, um Tastatureingaben zu lesen, und kann auch Zeilen aus einer Datei lesen, wenn Redirection verwendet wird.
read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
Parameterbeschreibung:
1, simple read
#!/bin/bash #Here it will be a newline by default 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: de.oldtoolbag.com The website name you entered is de.oldtoolbag.com
2,-p parameter allows to specify a prompt 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:de.oldtoolbag.com The website name you entered is de.oldtoolbag.com
3,-t parameter specifies the number of seconds 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, besides input 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 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-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 An input will exit:
#!/bin/bash read -n2 -p "Please enter any two characters: " any echo "\nYou entered the two characters are:$any" exit 0
Enter two characters to execute the program:
Please enter any two characters: 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
The program does not display the password after entering the password:
Please enter your password: The password you entered is w3codebox
6.Read file
The read command will read a line of text each time it is called. When there are no more lines to read in the file, the read command will exit 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 used as the input of the read command, the value of the read command after > 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 Parameter, the following example input character a Press Tab Press the key and the relevant filenames (existing in the directory) will be output:
$ read -e -p "Enter the filename:" str Enter the filename: a a.out a.py a.pyc abc.txt Enter the filename: a