如何从 cmd 向 stderr 发送消息?
在标准 Windows .cmd 文件中,我可以通过执行以下操作将消息发送到 stdout:
echo message
How can I send a message to stderr?
示例:
假设我有一个包含以下内容的脚本parent.cmd:
call child.cmd 1>>stdout.log 2>>stderr.log
和一个包含以下内容的子脚本:
:: Writes a message to stdout
:: (which in the parent is piped to stdout.log)
echo message
:: Now I want to write a message to stderr
:: (which in the parent is piped to stderr.log)
???
In a standard windows .cmd file I can send messages to stdout simply by doing:
echo message
How can I send a message to stderr?
Example:
Let's say I have the script parent.cmd containing:
call child.cmd 1>>stdout.log 2>>stderr.log
and a child containing:
:: Writes a message to stdout
:: (which in the parent is piped to stdout.log)
echo message
:: Now I want to write a message to stderr
:: (which in the parent is piped to stderr.log)
???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试将 STDOUT 的句柄重定向到 STDERR 的句柄。在您的子进程中,使用句柄重定向执行 echo:
Microsoft 参考文献:
You can try redirecting the handle of STDOUT to the handle of STDERR. In your child process, perform the echo with a handle redirect:
Microsoft references:
尝试
echo message 1>&2
我刚刚尝试过这个,它似乎工作正常,即使在批处理文件中也是如此。
Try
echo message 1>&2
I've just tried this and it seems to work correctly, even from within a batch file.
正如评论指出的:
将输出一个尾随空格。
正如 Dúthomhas 在评论中指出的那样,可以删除
1
,然后也可以删除空格。或者,可以将重定向放在命令的开头:
As a comment pointed out:
would output a trailing space.
As Dúthomhas pointed out in a comment, it is possible to drop the
1
and then the space can also be dropped.Alternatively, the redirect can be put at the start of the command: