Shell_read reads standard input

Source: Internet
Author: User

Shell_read reads standard input 1. The basic read command receives 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 your name:" // the function of parameter-n is not to wrap, echo is a newline read name by default // input echo "hello $ name, welcome to my program" from the keyboard // display information exit 0 // 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 my program" exit 0 has only one name variable after read, you can also have multiple data entries. If multiple data entries are input, the first data is given to the first variable, and the second data is given to the second variable. If too many data entries are input, 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 your name:" 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 read line // 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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.