1. Here we will summarize how shell reads files.
A), # Use the READ command to read a row of data while read mylinedo echo "line:" $ mylinedone <datafile.txt B ), # Use the READ command to read a row of Data cat datafile.txt | while read mylinedo echo "line:" $ mylinedonec), # Read a row of Data cat datafile.txt | while myline = $ (line) do echo "line:" $ mylinedoned), # Read a row of data while myline = $ (line) Do echo "line:" $ mylinedone <datafile.txt E ), # Use the READ command to read variable data cat datafile.txt | while read paraa parab paracdo echo "paraa:" $ paraa echo "parab:" $ parab echo "parac:" $ paracdonef ), # Use the READ command to read variable data while read paraa parab paracdo echo "paraa:" $ paraa echo "parab:" $ parab echo "parac:" $ paracdone <datafile.txt
G ),
The following is what we encountered when learning the company's code. In fact, it cannot be regarded as reading files. It should be read from standard input. The Code is as follows:
#!/bin/sh ip=192.168.253.111while read line <&3 ; do echo " attempt with ($line)" # Try to connect and exit when done if it worked. $line && exit 0done 3<<EOF/usr/bin/rlogin -l snap-admin $ip/usr/bin/ssh dev@$ip/usr/bin/ssh snap-admin@$ip/usr/bin/ssh root@$ipEOF
The following format is explained on the Internet:
<EOF
(Content)
EOF
Replace EOF with other things
It means to pass the content to the program as a standard input.
In this example, write 3 <EOF to redirect it to a file descriptor. We all know that the file descriptor is an integer. Here 3 is used as a file descriptor.
Here, we will briefly review the usage of <. When s h e l sees <, it will know that the next word is a separator. Content after the delimiters is treated as input until s h e l sees the delimiters (in a separate row ). This delimiter can be any string you define.
The following describes how to sort out common file descriptor commands:
Command> filename redirects the standard output to a new file. Command> filename redirects the standard output to a file (append) command 1> filename redirects the standard output to a file command> filename 2> & 1 redirects the standard output and standard error together to a file command 2> filename redirects the standard error command 2> filename in a file redirects a standard error to a file (append) command> filename 2> & 1 redirects the standard output and standard error together to a file (append) command <FILENAME> filename2 command uses the filename file as the standard input, use the filename2 file as the standard output command <FILENAME command using the filename file as the standard input command <delimiter reads from the standard output, until the delimiter command <& M uses the file descriptor m as the standard output command> & M redirects the standard output to the file descriptor M command <&-Disable the standard input
2. Use the READ command as follows:
The read command reads a row from the standard input and assigns the values of each field (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.txtwhile read -u 6 mylinedo echo "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 $ Sina
123456789
# Use the READ command to read variable data while read paraa parab parac paraddo echo "paraa:" $ paraa echo "parab:" $ parab echo "parac:" $ parac echo "Parad: "$ paraddone <datafile.txt: paraa: baiduparab: googleparac: tencentparad: sinaparaa: 123456789 parab: parac: