Linux cmd 2>&1 > 日志 vs cmd > 日志 2>&1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4699790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cmd 2>&1 > log vs cmd > log 2>&1
提问by dogbane
What is the difference between:
有什么区别:
cmd > log 2>&1
and
和
cmd 2>&1 > log
where cmd is a command?
cmd 在哪里是命令?
Which should I prefer and why?
我应该更喜欢哪个,为什么?
采纳答案by John Kugelman
Order matters. The way to reason about redirections is to read them from left to right and realize that redirections make streams point at the same place. They don't make streams point at each other.
订单很重要。推理重定向的方法是从左到右阅读它们并意识到重定向使流指向同一个位置。他们不会让流相互指向。
What does that mean? If you say 2>&1
then you are redirecting stderr to wherever stdout is currently redirected to. If stdout is going to the console then stderr is, too. If stdout is going to a file then stderr is as well. If you follow this up by then redirecting stdout, stderr still points to what stdout usedto point to. It does not "follow" stdout to the new location.
这意味着什么?如果你说2>&1
那么你正在将 stderr 重定向到 stdout 当前重定向到的任何地方。如果 stdout 要进入控制台,那么 stderr 也是。如果 stdout 将转到一个文件,那么 stderr 也是如此。如果您通过重定向 stdout 来跟进这一点,stderr 仍然指向 stdout曾经指向的内容。它不会“跟随”标准输出到新位置。
Right
对
cmd > log 2>&1
This redirects stdout to log
and then redirects stderr to wherever stdout is now being redirected, which is log
.
这会将 stdout 重定向到log
,然后将 stderr 重定向到现在重定向 stdout 的任何地方,即log
.
End result:both stdout and stderr are redirected to log
.
最终结果:stdout 和 stderr 都被重定向到log
.
Wrong
错误的
cmd 2>&1 > log
This redirects stderr to wherever stdout is currently being redirected, which is typically the console. Then stdout is redirected to log
. Remember that stderr does not "follow" stdout, so it continues to redirect to the console.
这会将 stderr 重定向到当前重定向 stdout 的任何地方,通常是控制台。然后标准输出被重定向到log
. 请记住,stderr 不会“跟随”stdout,因此它会继续重定向到控制台。
End result:stdout is redirected to the log file and stderr is (still) sent to the console. This is almost certainly not what you want.
最终结果:stdout 被重定向到日志文件,stderr 被(仍然)发送到控制台。这几乎肯定不是您想要的。
回答by Marcus Borkenhagen
cmd > log 2>&1
Redirects STDOUT
to log and than replaces STDERR
with the redirected STDOUT
.
重定向STDOUT
到日志,然后替换STDERR
为重定向的STDOUT
.
cmd 2>&1 > log
Replaces STDERR
with STDOUT
and then redirects the originalSTDOUT
to log.
替换STDERR
为STDOUT
然后将原始文件重定向STDOUT
到日志。