且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

cmd 2>&1>日志 vs cmd >日志 2>&1

更新时间:2022-04-18 04:47:17

订单很重要.推理重定向的方法是从左到右阅读它们并意识到重定向使流指向同一个位置.它们不会让流相互指向.

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.

这是什么意思?如果您说 2>&1 ,那么您将 stderr 重定向到 stdout 当前重定向到的任何位置.如果 stdout 将进入控制台,那么 stderr 也是如此.如果 stdout 将转到一个文件,那么 stderr 也是如此.如果您在重定向 stdout 之后跟进此操作,则 stderr 仍指向 stdout used 指向的内容.它不会跟随"标准输出到新位置.

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 used to point to. It does not "follow" stdout to the new location.

cmd > log 2>&1

这会将 stdout 重定向到 log,然后将 stderr 重定向到现在重定向 stdout 的位置,即 log.

This redirects stdout to log and then redirects stderr to wherever stdout is now being redirected, which is log.

最终结果: stdout 和 stderr 都被重定向到 log.

End result: both stdout and stderr are redirected to log.

cmd 2>&1 > log

这会将 stderr 重定向到当前重定向 stdout 的任何地方,通常是控制台.然后标准输出被重定向到 log.请记住,stderr 不会跟随"stdout,因此它会继续重定向到控制台.

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.

最终结果: stdout 被重定向到日志文件,stderr 被(仍然)发送到控制台.这几乎肯定不是您想要的.

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.