Centos redirection command

Source: Internet
Author: User

Centos redirection command

Yw> outfile 2> & 1 $ cat outfile cat: oyyw: No file or directory

And

$ Cat oyyw 2> & 1> outfilecat: oyyw: No file or directory
Confuse

After collecting some information, the answer is as follows:

Order is very important! The command is executed from left to right, so: * for the first command, shell first reads>outfile(Equivalent1>outfile).Standard outputRedirected to fileoutfileAnd then shell reads2>&1.Standard Error outputRedirectedStandard output, That is, redirecting to a fileoutfileSo the result isStandard outputAndStandard Error outputAll are redirected to the fileoutfileMedium

  • For the second command, shell first reads2>&1.Standard Error outputRedirectedStandard output(Probably the console), and then shell reads>outfile(Equivalent1>outfile).Standard outputRedirected to fileoutfile, The result isStandard Error outputOutput to the console,Standard outputOutput to fileoutfileMedium

In the following example, we should be able to answer the above questions:

$ Cat learn_redirect.sh #! /Bin/bashfoo () {echo "This is stdout"; # The output is the standard output echo "This is stderr" 1> & 2; # output is standard error output} foo> errlog 2> & 1> outfile
[pms@jq-hadoop ~/workspace/ouyangyewei/learn_linux] eth0 = xxx$ sh learn_redirect.sh [pms@jq-hadoop ~/workspace/ouyangyewei/learn_linux] eth0 = xxx$ cat outfile This is stdout[pms@jq-hadoop ~/workspace/ouyangyewei/learn_linux] eth0 = xxx$ cat errlog This is stderr
The description is as follows:

foo >errlog 2>&1 >outfileThe shell execution sequence is from left to right. First, shell reads>errlog.Standard outputRedirected to fileerrlogAnd shell reads2>&1.Standard Error outputRedirectedStandard output(EquivalentStandard Error outputRedirected to fileerrlog), And shell reads>outfile.Standard outputRedirect to fileoutfileTherefore, the result isStandard Error outputOutput to fileerrlogMedium,Standard outputOutput to fileoutfileMedium

References

Http://www.gnu.org/software/bash/manual/bash.html#Redirections http://mywiki.wooledge.org/BashFAQ/055 http://docstore.mik.ua/orelly/unix/upt/ch45_21.htm http://docstore.mik.ua/orelly/unix/upt/ch08_13.htm

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.