Shell_2 & gt; & amp; 1 (redirection) problems are often found in some scripts, especially when calling crontab, the following commands call tmptestsh & gt; tmptestlog2 & gt; & amp; the first half of tmptestsh & gt; tmptestlog is easy to understand, so the next 2 & gt; & amp; 1 is shell_2> & 1 (redirection) you can often find the following command to call/tmp/test in some scripts, especially when calling crontab. sh>/tmp/test. log 2> & 1 first half/tmp/test. sh>/tmp/test. log is easy to understand, so what is next to 2> & 1? To solve this problem, we still need to mention file redirection. We know> and <是文件重定向符。那么1和2是什么? 在shell中,每个进程都和三个系统文件相关联: 标准输入stdin 文件描述符分别为1 标准输出stdout文件描述符分别为2 标准错误stderr文件描述符分别为3 所以这里2> & 1 means to output standard errors to standard output. The following uses an example to demonstrate the functions of 2> & 1: $ cat test. shtdatetest. 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. by default, it is output to stdout and the time is displayed. . /Test. sh> test1.log. /test. sh: line 1: t: command not found $ cat test1.logTue Oct 9 20:51:50 CST 2007date execution results are redirected to the log file, and t cannot execute errors printed on the screen. Followed by 2> & 1 $. /test. sh> test2.log 2> & 1 the screen shows nothing $ cat test2.log. /test. sh: line 1: t: command not foundTue Oct 9 20:53:44 CST 2007stderr contents are also redirected to the log file. In fact,> is equivalent to 1> (the default value is standard output, so you can save writing), that is, 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. If you only want to redirect a standard error to a file, you can use 2> file name.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.