<, <,>,>
After a file descriptor is started in linux, three file descriptors are opened by default, namely standard input 0, standard output 1, error output 2, and open the file. The new file binding descriptor can be added in sequence. A shell command inherits the file descriptor of the parent process. Therefore, all running shell commands have three default file descriptors. Common Input and Output operators in linux shell are: 1. standard input (stdin): the code is 0. Use <or </dev/stdin->/proc/self/fd/0 0 to indicate:/dev/stdin 2. standard output (stdout): the code is 1. Use> or>;/dev/stdout->/proc/self/fd/1 to indicate: /dev/stdout3. standard error output (stderr): the code is 2, Use 2> or 2>;/dev/stderr->/proc/self/fd/2 2 stands: /dev/stderr >,>> output redirection: redirects the output to the screen, device, or file. Let's look at some examples.
Qiweijie @ qiweijie :~ /Study_shell $ ls t.txt 1ls: Lawless t.txt 1: no such file or directory t.txt # The correct output and error output are displayed on the screen # We try to redirect the terminal output to suc.txt (success) and we can see that there is only error output on the screen, # Here we can also see that the correct output by default (the code is 1,) can be ignored without writing qiweijie @ qiweijie :~ /Study_shell $ ls t.txt 1> suc.txt ls: Lawless t.txt 1: no such file or directory qiweijie @ qiweijie :~ /Study_shell $ cat suc.txt t.txt # correct output and error output are output to different files respectively. qiweijie @ qiweijie :~ /Study_shell $ ls t.txt 1> suc.txt 2> err.txt qiweijie @ qiweijie :~ /Study_shell $ cat suc.txt err.txt t.txt ls: Lawless t.txt 1: there is no such file or directory #> in this case, the file is appended to qiweijie @ qiweijie :~ /Study_shell $ ls t.txt 1> suc.txt 2> err.txt qiweijie @ qiweijie :~ /Study_shell $ cat suc.txt err.txt t.txtt.txt ls: Lawless t.txt 1: no such file or directory ls: Lawless t.txt 1: no such file or directory #&[-, 1, 2] indicates disabling the corresponding identifier, correct output, and error output # Disable Error output qiweijie @ qiweijie :~ /Study_shell $ ls t.txt 1 2> &-t.txt # The error output is redirected to/dev/nullqiweijie @ qiweijie :~ /Study_shell $ ls t.txt 1 2>/dev/nullt.txt # redirect the error output to the file qiweijie @ qiweijie :~ /Study_shell $ ls t.txt 1 2> err1.txtt.txt # disable the correct output and error output qiweijie @ qiweijie :~ /Study_shell $ ls t.txt 1> &-2> &-# output to/dev/nullqiweijie @ qiweijie :~ /Study_shell $ ls t.txt 1>/dev/null 2>/dev/null # redirect the error output to the correct output, and redirect the correct output to/dev/nullqiweijie @ qiweijie :~ /Study_shell $ ls t.txt 1>/dev/null 2> & 1 qiweijie @ qiweijie :~ /Study_shell $ ls t.txt 1>/dev/null 2> & 1 qiweijie @ qiweijie :~ /Study_shell $ ls t.txt 1> ttt.txt 2> & 1 qiweijie @ qiweijie :~ /Study_shell $ cat ttt.txt ls: Lawless t.txt 1: no such file or directory t.txt # & stands for all outputs qiweijie @ qiweijie :~ /Study_shell $ ls t.txt 1 &>/dev/nullqiweijie @ qiweijie :~ /Study_shell $ ls t.txt 1 &> te.txt qiweijie @ qiweijie :~ /Study_shell $ cat te.txt ls: Lawless t.txt 1: no such file or directory t.txt
Input redirection <, <let's look at some examples.
# The cat command gets the input from the screen and then outputs qiweijie @ qiweijie :~ /Study_shell $ catsadfsadf # The simplest example is to get the input from the screen and redirect it to qwj (here is the output redirection qiweijie @ qiweijie :~ /Study_shell $ cat> qwjqiweijie shi 2Bqiweijie @ qiweijie :~ /Study_shell $ cat qwj qiweijie shi 2B # obtain the input from the file qwj and redirect the cat input to qwj1qiweijie @ qiweijie :~ /Study_shell $ cat> qwj1 <qwj qiweijie @ qiweijie :~ /Study_shell $ cat qwj1qiweijie shi 2B # <two smaller numbers are connected together to accept an input end ID. When the input ID ends, enter qiweijie @ qiweijie :~ /Study_shell $ cat> qq <eof> q> w> j> eofqiweijie @ qiweijie :~ /Study_shell $ cat qqqwjqiweijie @ qiweijie :~ /Study_shell $ cat> qqq <e> q> w> eqiweijie @ qiweijie :~ /Study_shell $ cat qqqqwqiweijie @ qiweijie :~ /Study_shell $
Exec binding redirection
# Qiweijie @ qiweijie :~ /Study_shell $ ls/proc/self/fd0 1 2 3 # bind a new file descriptor 6 and bind the standard output (equivalent to saving a temporary copy for the standard output) qiweijie @ qiweijie: ~ /Study_shell $ exec 6> & 1qiweijie @ qiweijie :~ /Study_shell $ ls/proc/self/fd0 1 2 3 6 # bind the standard output to suc.txt qiweijie @ qiweijie :~ /Study_shell $ exec 1> suc.txt # No standard output is found. qiweijie @ qiweijie :~ /Study_shell $ ls-al # Suddenly found that suc.txt is already the output file qiweijie @ qiweijie :~ /Study_shell $ cat suc.txt cat: suc.txt: the input file is the output file qiweijie @ qiweijie :~ /Study_shell $ ls/proc/self/fd # bind the standard output to qiweijie @ qiweijie :~ /Study_shell $ exec 1> & 6qiweijie @ qiweijie :~ /Study_shell $ ls/proc/self/fd0 1 2 3 6 # disable the custom file descriptor qiweijie @ qiweijie :~ /Study_shell $ exec 6> &-qiweijie @ qiweijie :~ /Study_shell $ ls/proc/self/fd0 1 2 3qiweijie @ qiweijie :~ /Study_shell $ cat suc.txt total usage 128drwxrwxr-x 2 qiweijie 4096 November 24 10:50. drwxr-xr-x 34 qiweijie 4096 November 24 10:06 .. -rw-r -- 1 qiweijie 61 Aug 17 14:19 atlga. xml-rw-r -- 1 qiweijie 35 Aug 17 14:16 atlga. xml. old-rw-r -- 1 qiweijie 13 November 17 16:17 dos-file.txt-rw-rw-r -- 1 qiweijie 52 November 24 10:11 err1.txt-rw-r -- 1 qiweijie 104 1 January 24 10:09 err.txt-rw-r -- 1 qiweijie 41 November 18 14:20 example.txt-rwxrwxr-x 1 qiweijie 94 November 17 16:56 finduser-rw-r -- 1 qiweijie 0 November 24 10:50g-rw-r -- 1 qiweijie 324 November 18 14:57 merge-sales.sh-rw-rw-r -- 1 qiweijie 271 November 18 17:08 my-friends-rw-r -- 1 qiweijie 140 November 23 17:43 my-friends.sh-rwxrwxr-x 1 qiweijie 141 November 1 7 13:54 nusers-rw-r -- 1 qiweijie 1958 November 18 10:48 passwd.txt-rw-r -- 1 qiweijie 6 November 24 qq-rw-r -- 1 qiweijie qiweijie 4 October 10:49 qqq-rw-r -- 1 qiweijie 35 October 14:54 quotas-rw-r -- 1 qiweijie 16 October 10:46 qwj-rw-r -- 1 qiweijie 16 November 24 10:47 qwj1-rw-rw-r -- 1 qiweijie 39 November 18 14:53 sales-rw-r -- 1 qiweijie qi Weijie 0 11:00 suc.txt-rw-r -- 1 qiweijie 58 10:40 November 24 te.txt-rwxrwxr-x 1 qiweijie 55 November 24 17:02 trace1.sh-rw-r -- 1 qiweijie november 24 10:31 ttt.txt-rw-r -- 1 qiweijie 23733 November 18 15:52 t.txt-rw-r -- 1 qiweijie 14 November 17 16:18 unix-file.txt-rw-rw-r -- 1 qiweijie 13 November 17 16:20 unix.txt -rw-r -- 1 qiweijie 25, November 17 un x-file.txt 01236 # If you directly bind the standard output to a file, the problem is that you will never be back. qiweijie @ qiweijie :~ /Study_shell $ exec 1> suc.txt qiweijie @ qiweijie :~ /Study_shell $ cat suc.txt cat: suc.txt: the input file is the output file qiweijie @ qiweijie :~ /Study_shell $ exec 1> &-qiweijie @ qiweijie :~ /Study_shell $ cat suc.txt cat: standard output: incorrect file descriptor qiweijie @ qiweijie :~ /Study_shell $ ls/proc/self/fdls: write error: incorrect file descriptor qiweijie @ qiweijie :~ /Study_shell $
I believe that through these examples, we should be impressed with the file descriptor and <, <, >>,>.