如何从cmd向stderr发送消息?

ste*_*hin 54 cmd batch-file

在标准的Windows .cmd文件中,我只需执行以下操作即可向stdout发送消息:

echo message
Run Code Online (Sandbox Code Playgroud)

如何向stderr发送消息?

示例:

假设我的脚本parent.cmd包含:

call child.cmd 1>>stdout.log 2>>stderr.log
Run Code Online (Sandbox Code Playgroud)

和一个孩子包含:

:: 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)
???
Run Code Online (Sandbox Code Playgroud)

dol*_*phy 63

您可以尝试将STDOUT的句柄重定向到STDERR的句柄.在子进程中,使用句柄重定向执行回显:

:: 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)
echo message 1>&2
Run Code Online (Sandbox Code Playgroud)

微软'splainin它


小智 19

尝试 echo message 1>&2

我刚试过这个,它似乎工作正常,即使是在批处理文件中也是如此.