Introduction to file descriptors：The file descriptor is a non negative integer in form. Each file descriptor corresponds to an open fileFile descriptors are used to access files. The most well-known file descriptors are stdin (standard input), stdout (standard output), stderr (standard error). The system reserves three file descriptors 0, 1, 2 for them respectively in advance. We can also assign file descriptors to our files by special commands.
Meaning of redirection: in Linux, IOredirectIs to transfer the contents of one file descriptor to another specified file descriptor,Usually associated with file descriptors.
Most commonly, we can redirect the content of standard output to a specified file. Redirection operators (> and > > let you send output to a file, not to a terminal. >And > > are slightly different, although both can redirect text to a file,But the former will empty the file and then write the content, which willAppendTo the end of an existing file,By default, the redirection operator is for standard output, so > is equal to 1 >, similar > > is equal to 1 > >.
1: standard output and standard error redirection
3: custom file descriptor
1: for example, the echo command is to send the specified string to the standard output, so we can send the specified string to the specified file in combination with the echo command:
In this case, the contents of the file descriptor stdout (1) are redirected to the test.txt file through the redirection operator.
When processing errors, the output from stderr is dumped into the file / dev / null, which is a special device file and discards any received data. It’s also often called a black hole, because the data that enters it will be gone forever. We can also redirect the standard error output to a file for saving.
Next, this command will print stderr text to the screen instead of redirecting it to the text, because by default, the redirection operator is for standard output, where stdout does not output, but stderr output
You can redirect stderr by specifying the stderr file descriptor before the redirection operator
2: the command reading input from stdin can receive data in many ways:
With the less than sign, we can read the data in the file just like using stdin: CMD
When using pipes, the standard output of the previous command is often redirected to the standard input of the next command, CMD1 | cmd2.
3: use the exec command to create a file descriptor for reading (symbol & indicates that the number following is a file descriptor):
Create a file descriptor for writing:
4: the standard output can be redirected to a file or transmitted to other programs through a pipeline, but it cannot be done at the same time. The command tee can implement this operation. The command tee reads from stdin, redirects data to a file, and then redirects data to the standard output (stdout): CMD | tee file1 File2 | othercmd
For example, the results of LS command listing and cat command viewing file contents are standard output:
-A specifies to save to file as append
This knowledge point needs to understand the exec command, the opening mode of the file, and the preliminary understanding of the standard input and output.