linux shell "2> & 1" means - a perfect understanding - fee yuan Star

Master, round trip, first to understand.

 

Script:
      nohup / mnt / NAND3 / H2000G> / dev / null 2> & 1 &
      for & 1 should be more accurate to say that file descriptor 1, and 1 generally represents is STDOUT_FILENO, in fact, this operation is a dup2 (2 ) call. he standard output to all_result, and then copy the standard output to a file descriptor 2 (STDERR_FILENO), its consequence is a file descriptor and 2 refer to the same file table entry 1, it can be said error output was incorporated with 0 1 represents a key input screen output 2 represents an error output. the standard error output redirected to the standard, and then thrown to / DEV / NULL below. In layman's terms, that is, all standard output and standard error are thrown into the trash inside.
      command> out.file 2> &. 1 &
      command> out.file is out.file command to redirect the output file, i.e. the print content is not output to the screen, but is output to out.file file. 2> & 1 is to redirect standard error to standard output, standard output has been redirected to here out.file file is about to be output to the standard error out.file file. The last & is to allow the command in the background.
       
      Imagine 2> 1 represents what, combined with the 2> represents the error redirection, and 1 represents an error redirected to a file, and do not represent the standard output;
into 2> & 1, 1 & binding represents the standard output, it becomes redirected to the standard error output.

      you can use
            ls 2> 1 test, no errors will not be reported 2 files, but will output an empty file 1;
            LS xxx 2>
            ls xxx 2> & 1 test, this does not generate a document, but the standard output went wrong;
            LS XXX> out.txt 2> & 1, may in fact be replaced ls xxx 1> out.txt 2> & 1; weight directional sign> the default is 1, error, and outputs the spread out.txt.
      Why 2> & 1 write in the back?
      Command> file 2> & 1 
       First Command> file redirecting standard output to file 2> & 1 is the standard error copy behavior standard output, which is likewise redirected to file, the final result is the standard output, and error They are redirected to file. 
      2 Command> &. 1> File 
      2> & copy. 1 standard error of the output behavior of the standard, but this time in a standard output or terminal. > file after the outputs have been redirected to a file, but the standard error remain in the terminal.

Can be seen by the strace:
1. Command> File 2> &. 1
call redirection system key sequences in this command is:
Open (File). 3 ==
dup2 (3,1)
dup2 (1,2)

2. Command 2 > & 1> file
invoke this command sequence redirection critical systems are:
dup2 (1,2)
Open (File). 3 ==
dup2 (. 3, 

      1) can consider different dup2 () call sequence will produce what kind of file-sharing structure. Please refer to APUE 3.10, 3.12

Script:
      nohup / mnt / NAND3 / H2000G> / dev / null 2> & 1 &
      for & 1 should be more accurate to say that file descriptor 1, and 1 generally represents is STDOUT_FILENO, in fact, this operation is a dup2 (2 ) call. he standard output to all_result, and then copy the standard output to a file descriptor 2 (STDERR_FILENO), its consequence is a file descriptor and 2 refer to the same file table entry 1, it can be said error output was incorporated with 0 1 represents a key input screen output 2 represents an error output. the standard error output redirected to the standard, and then thrown to / DEV / NULL below. In layman's terms, that is, all standard output and standard error are thrown into the trash inside.
      command> out.file 2> &. 1 &
      command> out.file is out.file command to redirect the output file, i.e. the print content is not output to the screen, but is output to out.file file. 2> & 1 is to redirect standard error to standard output, standard output has been redirected to here out.file file is about to be output to the standard error out.file file. The last & is to allow the command in the background.
       
      Imagine 2> 1 represents what, combined with the 2> represents the error redirection, and 1 represents an error redirected to a file, and do not represent the standard output;
into 2> & 1, 1 & binding represents the standard output, it becomes redirected to the standard error output.

      you can use
            ls 2> 1 test, no errors will not be reported 2 files, but will output an empty file 1;
            LS xxx 2>
            ls xxx 2> & 1 test, this does not generate a document, but the standard output went wrong;
            LS XXX> out.txt 2> & 1, may in fact be replaced ls xxx 1> out.txt 2> & 1; weight directional sign> the default is 1, error, and outputs the spread out.txt.
      Why 2> & 1 write in the back?
      Command> file 2> & 1 
       First Command> file redirecting standard output to file 2> & 1 is the standard error copy behavior standard output, which is likewise redirected to file, the final result is the standard output, and error They are redirected to file. 
      2 Command> &. 1> File 
      2> & copy. 1 standard error of the output behavior of the standard, but this time in a standard output or terminal. > file after the outputs have been redirected to a file, but the standard error remain in the terminal.

Can be seen by the strace:
1. Command> File 2> &. 1
call redirection system key sequences in this command is:
Open (File). 3 ==
dup2 (3,1)
dup2 (1,2)

2. Command 2 > & 1> file
invoke this command sequence redirection critical systems are:
dup2 (1,2)
Open (File). 3 ==
dup2 (. 3, 

      1) can consider different dup2 () call sequence will produce what kind of file-sharing structure. Please refer to APUE 3.10, 3.12

Guess you like

Origin www.cnblogs.com/feiyuanxing/p/11612880.html