Linux readIntroduction
1. Basic read
The read command receives input from standard input (keyboard) or other file descriptors. 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 the line, and echo is to wrap the line by default.
Read name // input from the keyboard
Echo "Hello $ name, welcome to my program" // display information
Exit 0 // exit ShellProgram.
//********************************
Because the READ command provides the-p parameter, you can specify a prompt directly in the READ command line.
Therefore, the preceding 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
There is only one or more variables after the read operation. 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, all the values are given to the first variable. If there are too few inputs, it will not end.
//************************************** ***
You can also leave the variable unspecified in the READ command line.;If no variable is specified, the READ command places the received data in the environment variable reply.
For example:
Read-P "enter a number"
The environment variable reply contains all input data. You can use the environment variable reply in a shell script just 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 time input, 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
In this example, the-n option is used, followed by the value 1, indicating that the READ command will exit as long as it receives one character. 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 script user input is required, but the input data is not expected 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 enables the data input in the READ command not to be displayed on the monitor (in fact, the data is displayed, only the read Command to set 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 files
Finally, you can 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 transmit the result to the while command containing the READ command through the pipeline.
Example:
#! /Bin/bash
Count = 1// Value assignment statement without spaces
CAT test | while read line // the output of the cat command serves as the input of the read command,The read value is placed in line.
Do
Echo "line $ count: $ line"
Count = $ [$ count + 1] // note the spaces in brackets.
Done
Echo "finish"
Exit 0
Original
Http://www.cnblogs.com/iloveyoucc/archive/2012/04/16/2451328.html