1 -- shell Method for reading files
# Use the READ command to read a row of data while read mylinedoecho "line:" $ mylinedone <datafile.txt
# Use the READ command to read a row of Data cat datafile.txt | while read mylinedo echo "line:" $ mylinedone
# Read a row of Data cat datafile.txt | while myline = $ (line) Do echo "line:" $ mylinedone
# Read a row of data while myline = $ (line) Do echo "line:" $ mylinedone <datafile.txt
# Use the READ command to read variable data cat datafile.txt | while read paraa parab paracdoecho "paraa:" $ paraaecho "parab:" $ parabecho "parac:" $ paracdone
# Use the READ command to read variable data while read paraa parab paracdoecho "paraa:" $ paraaecho "parab:" $ parabecho "parac:" $ paracdone <datafile.txt
2 -- How to Use the READ command
READ command fromStandard InputRead a row and assign the values of each field in the input line (separated by a specified separator) to the variables on the command line.
Read [-ers] [-u FD] [-T timeout] [-P prompt] [-A array] [-N nchars] [-D delim] [name...]
Parameter Parsing:
-E |
|
-R |
The read command uses "\" (backslash) as a common character in the input line, rather than a control character. |
-S |
Quiet Mode. If this parameter is specified and data is read from the terminal, the input will not be displayed on the screen. |
-U <FD> |
Specifies the file descriptor for reading data. The default standard input is no longer used. |
-T <timeout> |
Wait for the timeout time of the standard input, in seconds. If no input is made within the specified time period, the system returns the result immediately. |
-P <prompt> |
Print the prompt, wait for the input, and assign the input value to the reply variable or the specified variable. |
-A <array> |
Reads a group of words and assigns values to the array in turn. |
-N <nchars> |
Read the specified number of characters. If n characters have been read, return immediately. Continue reading from the return point next time. If n characters have been read to the end of the line, return regardless of whether n characters are full. |
-D <delim> |
Specifies the row Terminator. Reading this character serves as the end of a row. |
Name... |
Specify the read variable. After reading a row of data, read splits the row data into fields, and then assigns the fields to the specified variables in sequence. If the number of fields separated is more than the specified variable, all the remaining field values are assigned to the last variable. Otherwise, the remaining variables are assigned a null string. If no variable is specified for read, the system uses the default reply as the default variable name. |
Use redirection to read data
Exec 6 <datafile.txt while read-U 6 mylinedoecho "line:" $ mylinedone
Variable Separator
The default delimiter of the read command is space. Multiple spaces are treated as one space. You can also use characters specified by IFS (internal field separator) as separators. If there is a file with the following content, which separates variables with "$" and wants to differentiate each variable, you can use the following script:
Baidu $ Google $ tencnt $ sina123456789
# Use the READ command to read variable data while read paraa parab parac paraddoecho "paraa:" $ paraaecho "parab:" $ parabecho "parac:" $ paracecho "Parad:" $ paraddone <datafile.txt
The output of the script is as follows:
Paraa: baiduparab: googleparac: tencentparad: sinaparaa: 123456789 parab: parac: Parad:
3-References
1. http://www.linuxsense.org/archives/63.html