Each opened file is assigned a file descriptor. stdin (keyboard), stdout (screen), and stderr (error messages are output to the screen) file descriptors are 0, 1, and 2. the descriptor 3 to 9 is retained for the extra files being opened.
Each opened file is assigned a file descriptor. stdin (keyboard), stdout (screen), and stderr (error message output to screen) file descriptors are 0, 1, and 2 respectively. the descriptor 3 to 9 is retained for the extra files being opened.
The file descriptor is a number assigned to the file system to track the opened file.
The following is an example
1,> Usage
[Zhangy @ localhost ~] $ Echo "11111"> 1.txt
[Zhangy @ localhost ~] $ Cat 1.txt
11111
[Zhangy @ localhost ~] $ Echo "11111" 1> 1.txt // this usage is the same as the above usage
[Zhangy @ localhost ~] $ Cat 1.txt
11111
[Zhangy @ localhost ~] $ Echo "22222"> 1.txt
[Zhangy @ localhost ~] $ Cat 1.txt
22222
I think everyone is familiar with this usage, because it is often used by me.Echo "11111"> 1.txt,> there is a 1 above, which is the default.> When redirected to a file, the file is cleared and written.
In this example, you can clear the content in the 1.txt file. In fact, it is easy to understand. after clearing the file, it is of course empty if you do not enter the content.
2,> Usage
[Zhangy @ localhost ~] $ Echo "2222"> 2.txt
[Zhangy @ localhost ~] $ Cat 2.txt
2222
[Zhangy @ localhost ~] $ Echo "2222"> 2.txt
[Zhangy @ localhost ~] $ Cat 2.txt
2222
2222
As shown in the preceding example,> When redirected to a file, the output is written to the end of the file. This is totally different from>.
3, 0, 1, 2, 9, and &
[Zhangy @ localhost ~] $ Cat 1.txt
333
111
222
444
[Zhangy @ localhost ~] $ Grep "22" 0 <1.txt // read the content in 1.txt
222
[Zhangy @ localhost ~] $ Ls tank 2> 1.txt // put the error message at the end of the file
[Zhangy @ localhost ~] $ Cat 1.txt
333
111
222
444
Ls: tank: there is no file or directory. // This is the content produced by 2> 1.txt.
[Zhangy @ localhost ~] $ (Ls tank; pwd) &> 1.txt // store the error information to the output content in the 1.txt file. & (stderr, stdout)
[Zhangy @ localhost ~] $ Cat 1.txt
Ls: tank: no file or directory
/Home/zhangy
I> & j redirect all output from file descriptor I to file j. pointing to file I is sent to file j. I and j are 0-9.
[Zhangy @ localhost ~] $ Ls tank 2> & 1 | egrep \ * 1> 2.txt // here, I redirected the error message to 1. here, 1 will output an error, and the root value is almost the same
[Zhangy @ localhost ~] $ Cat 2.txt
Ls: tank: no file or directory
4, <> Usage
<This symbol is read.> This symbol is output. it is opened and read when put together.
[Zhangy @ localhost ~] $ Echo 123546> 2.txt
[Zhangy @ localhost ~] $ Exec 4 <> 2.txt // open 2.txt and read the content to & 4
[Zhangy @ localhost ~] $ Read-n 4 <& 4 // read 4 characters from & 4
[Zhangy @ localhost ~] $ Echo-n.> & 4 // write a vertex to & 4
[Zhangy @ localhost ~] $ Exec 4> &-// Close output file descriptor 4
[Zhangy @ localhost ~] $ Cat 2.txt
1235.6
You can see clearly the role of the above example.
5, n <&-disable input file descriptor n> & disable output file descriptor n
[Zhangy @ localhost ~] $ Ls tank // An error is prompted
Ls: tank: no file or directory
[Zhangy @ localhost ~] $ Ls tank 2> &-// after I close the error, nothing is output.
6, | management command
This command is also commonly used. it is typical to use the output of one command as the input of another command.
[Zhangy @ localhost ~] $ Echo tank | wc-w
1
It indicates that tank is an output, but it is a wc input, otherwise there will be no statistics.