Shell redirects the standard error output to the standard output

Source: Internet
Author: User

Sometimes I want to get the output of a Write statement when writing some shell scripts. For example, when I use javac to compile a java class, I want to know whether the compilation is successful, it is easy to think of to judge based on the javac output result. Both the output and error messages are displayed on the terminal, but if we directly use javac Main. java | wc-l to judge (Main. java syntax errors). The final result is an error output, and the number of rows is counted as 0. In this case, we only need to redirect the error to the standard output, because wc and other commands only obtain the standard output stream through the pipeline. The correct syntax is as follows: javac Main. java 2> & 1 | wc-l. Linux stream definition: 0 standard input, 1 standard output, 2 standard error. 2> & 1 point the standard error stream to the standard output stream, so that we can determine the execution of the command based on the number of wrong lines or other information.

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.