In shell, the read parameter read command-n (no line feed)-p (prompt statement)-n (number of characters)-t (wait time)-s (no Echo) 1. read the read command to receive standard input (keyboard) or input of other file descriptors ). After the input is obtained, the read command returns the data shell read parameter read command-n (no line feed)-p (prompt statement)-n (number of characters)-t (wait time) -s (not echo) 1. read the read command to receive standard input (keyboard) or input of other file descriptors (as described later ). After the input is obtained, the read command puts the data into a standard variable. The following is the simplest form of the read command ::#! /Bin/bash echo-n "Enter yourname:" // The function of parameter-n is not line feed, and echo is line feed by default. read name // input echo "hello $ name, welcome to myprogram "// Display information exit0 // exit the shell program. -P parameter, you can specify a prompt in the read command line. Therefore, the above script can be abbreviated as the following script ::#! /Bin/bash read-p "Enter your name:" name echo "hello $ name, welcome to myprogram" exit 0 has only one name variable after read, there can also be multiple. if you enter multiple data, the first data is given to the first variable, and the second data is given to the second variable. if the number of input data is too large, then all the values are given to the first variable. If there are too few inputs, it will not end. //************************************** * ** You can also choose not to specify variables in the read command line. if no variable is specified, the read command places the received data in the environment variable REPLY. For example, the read-p "Enter a number" environment variable REPLY contains all the input data. you can use the environment variable REPLY in a shell script like other variables. 2. timing input. there is a potential risk of using the read command. The script is likely to stop waiting for user input. If the script must be executed no matter whether the data is entered, you can use the-t option to specify a timer. -T option specifies the number of seconds for the read command to wait for input. When the time is full, the read command returns a non-zero exit status ;#! /Bin/bash if read-t 5-p "please enter yourname:" name then echo "hello $ name, welcome to my script" else echo "sorry, too slow "fi exit 0 in addition to the input time, you can also set the read command to count the input characters. When the number of characters entered reaches the predefined number, the system 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 a value of 1, indicating that the read command will exit as long as one character is accepted. As long as the answer is followed by the next character, the read command immediately accepts the input and passes it to the variable. You do not need to press Enter. 3. silent reading (the input is not displayed on the monitor) sometimes requires script user input, but does not want the input data to be displayed on the monitor. A typical example is to enter a password. Of course there are many other data to be hidden. The-s option does not display the data entered in the read command on the monitor (in fact, the data is displayed, but the read command sets the text color to the same color as the background ). #! /Bin/bash read-s-p "Enter your password:" pass echo "your password is $ pass" exit 0 4. read the last part of the file, you can also use the read command to read files in Linux. Each time you call the read command, the "one line" text in the file is read. When the file has no readable rows, the read command exits in a non-zero state. The key to reading a file is how to transmit data in the text to the read command. The most common method is to use the cat command on the file and directly send the result to the while command example containing the read command through the pipeline ::#! /Bin/bash count = 1 // value assignment statement without spaces cat test | while readline // the output of the cat command is used as the input of the read command, put the read value in the line do echo "Line $ count: $ line" count = $ [$ count + 1] // note the spaces in the brackets. Done echo "finish" exit 0
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.