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 reads
2>&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