Each open file is assigned a file descriptor. stdin (keyboard), stdout (screen), and stderr (Error message output to screen) are the file descriptors 0, 1, and 2 respectively. The descriptor 3 to 9 is retained for the extra files that are being opened.
A file descriptor is a number assigned to it by the file system to track the open file.
The following examples illustrate
The use of 1,>
Copy Code code as follows:
[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 use above
[Zhangy@localhost ~]$ Cat 1.txt
11111
[Zhangy@localhost ~]$ echo "22222" >1.txt
[Zhangy@localhost ~]$ Cat 1.txt
22222
This usage I think everyone is very familiar with, because this is I often use. echo "11111" > 1.txt,> is preceded by a 1, which is the default. > When redirected to a file, the file is emptied and written.
Copy Code code as follows:
[Zhangy@localhost ~]$ > 1.txt
[Zhangy@localhost ~]$:> 1.txt
This example above, you can empty the contents of the 1.txt file, but also very good understanding, empty, not input content of course for empty.
The use of 2,>>
Copy Code code as follows:
[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
From the above example we can see that when,>> is redirected to a file, the output is written to the end of the file. This is totally different from >.
3,0,1,2 to 9, as well as &
Copy Code code as follows:
[Zhangy@localhost ~]$ Cat 1.txt
333
111
222
444
[Zhangy@localhost ~]$ grep 0< 1.txt//Read the contents of 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: Without that file or directory//This is what 2>> 1.txt produces.
[Zhangy@localhost ~]$ (LS tank;p wd) &>1.txt//Send error messages and output to 1.txt files,& (stderr,stdout)
[Zhangy@localhost ~]$ Cat 1.txt
Ls:tank: No file or directory
/home/zhangy
I>&j redirect File descriptor I to J. All output to the I file is sent to J. I and J are the number 0-9.
Copy Code code as follows:
[Zhangy@localhost ~]$ ls tank 2>&1|egrep \* 1> 2.txt//Here I redirect the error message to 1, where 1 will output the error, the root assignment is almost
[Zhangy@localhost ~]$ Cat 2.txt
Ls:tank: No file or directory
The use of 4,<>
This symbol is read,> This symbol is output, put together to open and read,
Copy Code code as follows:
[Zhangy@localhost ~]$ echo 123546 > 2.txt
[Zhangy@localhost ~]$ exec 4<> 2.txt//Open 2.txt and read content to &4
[Zhangy@localhost ~]$ read-n 4 <&4//Read 4 characters from &4
[Zhangy@localhost ~]$ echo-n. >&4//Writing a point to &4
[Zhangy@localhost ~]$ exec 4>&-//Close output File descriptor 4
[Zhangy@localhost ~]$ Cat 2.txt
1235.6
By going through the above example, you can clearly understand the role of <>
5,n<&-Close Input file descriptor n n>& turn off output file descriptor n
Copy Code code as follows:
[Zhangy@localhost ~]$ LS tank//will prompt for error
Ls:tank: No file or directory
[Zhangy@localhost ~]$ ls tank 2>&-//I turn the error off, nothing is exported
6,| Management Command
This command is also commonly used, and is typically the output of one command, as input to another command.
Copy Code code as follows:
[Zhangy@localhost ~]$ echo Tank | Wc-w
1
Indicates that the tank is an output, but it is also the input of WC, otherwise there will be no statistical appearance.