Shell programming 2 & gt; & amp; 1

Source: Internet
Author: User
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.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.