You can often find the following command to call tmptest in some scripts, especially when calling crontab. shtmptest. the first half of log21 tmptest. shtmptest. log is easy to understand, so what is the next 21? To solve this problem, we still need to mention file redirection. We know and are file redirection characters. So 1 and 2
You can often find the following command to call/tmp/test in some scripts, especially when calling crontab. sh/tmp/test. the first half of log 21/tmp/test. sh/tmp/test. log is easy to understand, so what is the next 21? To solve this problem, we still need to mention file redirection. We know and are file redirection characters. So 1 and 2
You can often find the following command calls in some scripts, especially when calling crontab:
/Tmp/test. sh>/tmp/test. log 2> & 1
The first half of/tmp/test. sh>/tmp/test. log is easy to understand. What is the next 2> & 1?
To solve this problem, we still need to mention file redirection. We know> and <是文件重定向符。那么1和2是什么?在shell中,每个进程都和三个系统文件相关联:标准输入stdin,标准输出stdout和标准错误stderr,三个系统文件的文件描述符分别为0,1和2。所以这里2> & 1 means to output standard errors to standard output.
The following uses an example to demonstrate the functions of 2> & 1:
$ Cat test. sh
T
Date
Test. sh contains two commands, t is a non-existent command, and an error is reported during execution. By default, the error is output to stderr. Date can be correctly executed and the time information is output. It is output to stdout by default.
./Test. sh> test1.log
./Test. sh: line 1: t: command not found
$ Cat test1.log
Tue Oct 9 20:51:50 CST 2007
We can see that the execution result of date is redirected to the log file, and the error t cannot be executed is only printed on the screen.
$./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.
Actually,>It is equivalent1>That is, the redirection standard output, excluding standard errors. With 2> & 1, the standard error is redirected to the standard output, and then the standard output and standard error information are redirected together with the redirection. You can use2> file.