Linux read syntax and analysis

Source: Internet
Author: User

Read command-N (no newline)-p (hint statement)-N (number of characters)-T (wait time)-s (not echoed)

1. Basic Reading
The read command receives input from the standard input (keyboard) or other file descriptor (which is said later). After the input is received, the Read command puts the data into a standard variable. Here is the Read command
In the simplest form:
#!/bin/bash
Echo-n "Enter Your Name:"//Parameter-n is not wrapped, echo defaults to line wrapping
Read name//input from keyboard
echo "Hello $name, Welcome to my program"//Display information
Exit 0//Quit Shell program.
//********************************
Because the Read command provides the-p parameter, it allows you to specify a prompt directly on the read command line.
So the script above can be simply written in the following script:
#!/bin/bash
Read-p "Enter Your name:" Name
echo "Hello $name, Welcome to my program"
Exit 0
In the above read after the variable only name one, there can be more than one, if you enter more than one data, then the first data to the first variable, the second data to the second variable, if the number of input data too many, then all the last value to the first variable. If too little input does not end.
//*****************************************
You can also specify no variables on the read command line. If you do not specify a variable, 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 the input data, and you can use the environment variable reply in your shell script just like you would with other variables.
2, timing input.
There is a potential risk of using the Read command. The script is likely to stop waiting for the user's input. You can use the-t option to specify a timer, whether or not the input data script must continue to execute.
The-t option specifies the number of seconds that the read command waits for input. When the timer is full, the Read command returns a non-0 exit state;
#!/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 entering time timings, you can also set the Read command count input character. Automatically exits when the number of characters entered reaches a predetermined number, and assigns the input data to the variable.
#!/bin/bash
Read-n1-p "does 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, which instructs the read command to exit as soon as it accepts a character. Just press one character to answer and the Read command immediately
Accept the input and pass it to the variable. No need to press ENTER.

3, silent reading (input not displayed on the monitor)
It is sometimes necessary to script user input, but you do not want the input data to appear on the monitor. A typical example is entering a password, and of course there are many other data that need to be hidden.
The-S option enables the data entered in the Read command to not appear on the monitor (in fact, the data is displayed, except that 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 file
Finally, you can also use the Read command to read files on a Linux system.
Each call to the Read command reads "one line" of text in the file. When a file does not have a readable row, the read command exits with a non-0 status.
The key to reading a file is how to transfer the data in the text to the Read command.
The most common approach is to use the Cat command for a file and pass the results directly to the while command that contains the read command
Example::
#!/bin/bash
Count=1//assignment statement, without spaces
Cat Test | While the output of the Read line//cat command is input to the Read command, the read value is placed in line

Summarize how the shell reads the file

a), #使用read命令读取一行数据 whileRead Myline Do      Echo "Line :"$myline Done<datafile.txt b), #使用read命令读取一行数据CatDatafile.txt | whileRead Myline Do       Echo "Line :"$myline done c), #读取一行数据CatDatafile.txt | whileMyline=$ (line) Do       Echo "Line :"$myline done D), #读取一行数据 whileMyline=$ (line) Do       Echo "Line :"$myline Done<datafile.txt e), #使用read命令读取变量数据CatDatafile.txt | whileRead Paraa parab parac Do      Echo "Paraa:"$paraaEcho "Parab:"$parabEcho "Parac:"$parac done f), #使用read命令读取变量数据 whileRead Paraa parab parac Do      Echo "Paraa:"$paraaEcho "Parab:"$parabEcho "Parac:"$parac Done< Datafile.txt

The following is actually not a read file, should be considered from the standard input read, the code is as follows:

#!/bin/SHIP=192.168.253.111   whileRead Line <&3; Do          Echo "attempt with ($line)"# Try to connect and exit when Done ifit worked. $line&& exit0   Done 3<<EOF/usr/bin/rlogin-l snap-Admin $ip/usr/bin/SSH[email protected] $ip/usr/bin/SSHsnap-[email protected] $ip/usr/bin/SSH[email protected] $ip EOF

The following format:
<<eof
Content
Eof

To replace EOF with something else.

It means to pass the content as standard input to the program.

In this case, the 3<<eof should be to redirect it to a file descriptor, and everyone knows that the file descriptor is an integer, and that 3 is used as a file descriptor.

Here is a brief look at the use of <<.
When the shell sees << , it knows that the next word is a delimiter.
The content after the delimiter is treated as input until the shell sees the delimiter again (on a separate line).
This delimiter can be any string that you define .

The following is a collation of common file descriptor commands:

Command >filename redirects the standard output to a new file command>>filename redirects the standard output to a file (append) command1>filename redirects the standard output to a file command> filename2>&1REDIRECT standard output and standard error to a file command2>filename redirects a standard error to a file command2>>filename redirects a standard error to a file (append) command>> filename2>&1REDIRECT standard output and standard error to a file (append) command< filename >The filename2 command commands the filename file as the standard input and the filename2 file as the standard output command<the filename command commands the filename file as the standard input command<<delimiter is read from the standard output until the delimiter delimiter command is encountered<&m The file descriptor m as the standard output command>&m redirect the standard output to the file Descriptor M command<&-Turn off standard input

2, the use of the Read command is organized as follows:

The read command reads a row from the standard input and assigns the value of each field in the input row (delimited by the specified delimiter) to a variable on the command line.

Read [-ers] [-u FD] [-t timeout] [-p prompt] [-a array] [-N nchars] [-D delim] [name ...]  

Parameter resolution:
-R
Specifies the read command to "\" (backslash) as an ordinary character of the input line, not a control character.
-S
Quiet mode. If this parameter is specified and the data is read from the terminal, the input will not be echoed on the screen.
-U <fd>
Specifies the file descriptor of the read-in data and no longer uses the default standard input.
-T <timeout>
waits for the standard input time-out, in seconds. Returns immediately if no input is entered within the specified time.
-P <prompt>
Print prompt, wait for input, and assign the input to the reply variable or to the specified variable.
-a <array>
reads a set of words and assigns them to an array of arrays, in turn.
-n <nchars>
reads the specified number of characters. If the n characters have been read, return immediately and continue reading the next time from the return point, if the end of the line is reached, no matter the full n characters Nonalphanumeric returned.
-D <delim>
Specifies the line terminator, which reads the character as the end of a line.
Name ... The
specifies the read variable. When read reads a row of data, separates the row data into individual fields, and then assigns the fields to the specified variables in turn. If the number of fields separated is more than the specified variable, then all the remaining field values are assigned to the last variable, whereas the remaining variables are assigned an empty string. If read does not specify a variable, the system uses the default reply as the default variable name.
using redirection to read data

6< datafile.txtwhile6  mylinedo    echo" line :'$myline Done

Variable delimiter
The default delimiter for the Read command is a space, and multiple spaces are treated as a single space. We can also use the characters specified by the IFS (inner field delimiter) as delimiters. If you have a file with the following, it separates variables with "$", and you want to differentiate each variable, you can use the following script:

Baidu$google$tencnt$sina

123456789

#使用read命令读取变量数据 whileRead Paraa parab parac Parad Do      Echo "Paraa:"$paraaEcho "Parab:"$parabEcho "Parac:"$paracEcho "Parad:"$parad Done<the output of the Datafile.txt execution script is as follows: Paraa:baidu parab:google parac:tencent parad:sina paraa:123456789Parab:parac:

http://blog.csdn.net/xj178926426/article/details/6925770

Linux read syntax and analysis

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.