Some tips on how to redirect in Linux I/O

  

Linux I/O redirection is very simple, but it is very useful in scripting and system management. It is very useful to find out how to use it.
First of all, what is I/O redirection? I/O redirection is simply a process that captures a file, or a code block in a command, program, script, or even script. The output is then sent as an input to another file, command, program, or script.
If you talk about I/O redirection, it involves the concept of File Descriptor. On Linux systems, the system assigns a file identifier to each open file so that the system can track the file. Some are similar to file handles in C programming. The file identifier is a number. Different numbers represent different meanings. By default, the system occupies 3, which are 0 standard input (stdin) and 1 standard output (stdout). , 2 standard error (stderr), and 3-9 are reserved identifiers, which can be specified as standard input, output or error as a temporary connection. This usually resolves many complex redirect requests.
Standard input usually refers to the input of the keyboard. The standard output usually refers to the output of the display.
Standard errors are usually directed to the display.
Please see the following example to describe their relationship:
#ls /dev< Br> This command lists all the files in the /dev directory and outputs the results on the screen.
Here /dev is the standard input of the command ls (entered from the keyboard), and the result printed on the screen is the standard output (the contents of the /dev directory) or back to the title, the redirect is the standard input or output Change to other ways, please refer to the following example or equivalent to
#ls /dev 1>filename #Note: There is no space between "1" and ">"
The above command will put the standard of the command The output is redirected to a file filename instead of being displayed on the screen. If the file identifier is not specified, the system defaults to 1, so 1 can be omitted.
If you change the above example, change ">" to " ;>>" means appending the output to the end of the filename file, creating it if the file does not exist. The following
#ls /dev >>filename
can also redirect standard errors to a file
#ls -qw /dev 2>filename
Obviously -qw is an error parameter, usually An error message is reported on the display, but since the 2 standard error (stderr) is redirected to the file filename, the display has no error message and the message is written to the file.
The following command directs the standard output and error to the file.
#ls /dev &>filename
"&" Here stands for standard output and standard error, where either the normal output or the error message is written to filename.
Redefining standard input, output, and error file identifiers
Redefining file identifiers can use the i>&j command to redirect the file identifier i to j, you can put "& ;" Understand as "get address"
See the following example:
#e xec 5>&1
means to direct file identifier 5 to standard output, this command is usually used to temporarily Save the standard input.
The same standard input can also be redirected, please refer to the following example
# grep search-word
In general, the grep command searches for a string in a given file. The above command uses the file filename as the grep command. Standard input instead of typing from the keyboard.
As mentioned earlier, the system assigns a file identifier to each open file so that the system can track the file. What is the default file identifier? The answer is 0, which is standard input, or it can be said to be input from the keyboard. Of course, this file identifier can also be specified by itself. Please refer to the following example:
#e cho 123456789 >filename Write the string to the file filename #e xec 3<>filename Open the file filename and specify the file identifier The character is 3 #read -n 4 <&3 reads 4 characters from the file, the handle has been pointed to the end of the fourth character #e cho -n . >&3 Write a point at the 5th character , covering the 5th character, -n means not wrapping #e xec 3>&- closing the file identifier
Now the result of the cat filename file becomes 1234.6789
The command j<>filename means to open the file And indicate that the file identifier is j
"&-" means to close the file identifier
For the operation of closing the file identifier, please refer to the following
n<&- Close the input file identifier n 0<&- or <&- close standard input stdin n>&- close output file identifier n 1>&- or>&-close standard output stdout
There are other commands , as below reference
> filename or > filename
means to set the file filename to empty, that is, to clear the contents of the file. If the file does not exist, create an empty file (equivalent to the touch command): indicates an empty output, two commands The only difference is that >filename does not work in all shells.

Copyright © Windows knowledge All Rights Reserved