Bat 将 stderr 重定向到 stdout 有奇怪的行为
我有一个 bat 脚本,它在某一时刻将进程的 stderr 重定向到 stdout,然后将其写入文件。我曾经这样做过:
process.exe 2>&1 > file.txt
但是,这不会将 stderr 重定向到文件(出于我无法理解的原因)。当我将该行修改为:
process.exe > file.txt 2>&1
整个事情就成功了。这两个不是等价的吗?
I have a bat script which at one point redirects the stderr of a process to the stdout, and then writes it to a file. I used to do it like this:
process.exe 2>&1 > file.txt
However, this doesn't redirect the stderr to the file ( for reasons I can't understand ). When I modified the line to :
process.exe > file.txt 2>&1
The whole thing worked. Aren't these two equivalent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一个示例本质上是这样的:
因此,stderr 仍然指向原始的 stdout。您的第二个示例是:
因此,
stderr
和stdout
现在都引用file.txt
。这是令人恼火的微妙。The first example essentially does:
So,
stderr
is still pointing at the originalstdout
. Your second example does:So, both
stderr
andstdout
now referencefile.txt
. It's annoyingly subtle.重定向
2>&1
在命令行末尾起作用。它不能作为第一个重定向参数,重定向需要文件名和末尾的2>&1
。您实际上正在尝试重定向 stderr,但没有占位符来存储 stderr 消息,因此失败。记住这一点的捷径是希望这会有所帮助,
此致,
汤姆.
The redirection
2>&1
works at the end of the command line. It will not work as the first redirection parameter, the redirection requires a filename and the2>&1
at the end. You're effectively trying to redirect stderr but there is no placeholder to store the stderr messages hence it failed. The shortcut to remembering this isHope this helps,
Best regards,
Tom.
顺便说一句,由于我不完全理解的原因,类似的思考
似乎也有效
By the way, for reasons I do not completely understand, a think like
also seems to work