http://blog.csdn.net/gyanp/article/details/7258356
It is often possible to find the following form of command invocation in some scripts, especially when Crontab calls
/tmp/test.sh >/tmp/test.log 2>&1
The first half of/tmp/test.sh >/tmp/test.log is easy to understand, so what's going on behind the 2>&1?
To explain the problem, you still have to refer to file redirection. We know that > and < are file redirection characters. So what are 1 and 2?
In the shell, each process is associated with three system files: standard input stdin, standard output stdout and standard error stderr, and three system files with 0, 1, and 2 file descriptors respectively. So the 2>&1 here means that the standard error is also output to the standard output.
Here's an example of how 2>&1 works:
$ cat test.sh
T
Date
test.sh contains two commands, where T is a non-existent command, execution will error, by default, errors will be output to stderr. Date is executed correctly and output time information, default output to stdout
./test.sh > Test1.log
./test.sh:line 1:t: Command not found
$ cat Test1.log
Tue Oct 9 20:51:50 CST 2007
As you can see, the execution result of date is redirected to the log file, and the error that T cannot execute is printed on the screen only.
$./test.sh > Test2.log 2>&1
$ cat Test2.log
./test.sh:line 1:t: Command not found
Tue Oct 9 20:53:44 CST 2007
This time, the contents of stderr and StdOut are redirected to the log file.
In fact, > is equivalent to 1> , which is the redirection standard output, and does not include standard errors. With 2>&1, the standard error is redirected to standard output, and then using > redirection redirects the standard output along with the standard error message. If you only want to redirect the standard error to a file, you can use the 2>file.
Such as:
nohup/mnt/nand3/h2000g >/dev/null 2>&1 &
For & 1 More accurate should be the file descriptor 1, and 1 is generally representative of Stdout_fileno, in fact, this operation is a dup2 (2) call. He standard output to All_result, and then copy the standard output to the file descriptor 2 (stderr_ Fileno), the consequence is that file descriptors 1 and 2 point to the same file table entry, or the wrong output is merged. where 0 means keyboard input 1 means that the screen output 2 indicates an error output. REDIRECT standard error to standard output, and then throw it under/dev/null. In layman's words, all standard output and standard errors are thrown into the trash.
Command >out.file 2>&1 &
Command >out.file redirects the command output to the Out.file file, which means that the output is not printed to the screen, but is output to the Out.file file. 2>&1 is redirecting standard errors to standard output, where the standard output has been redirected to the Out.file file, and the standard error is output to the Out.file file. The last & is to have the command execute in the background.
2>&1 Meaning of Shell programming