繁体   English   中英

在Windows命令提示符(cmd.exe)中管道输出失败的命令

[英]Piping output of a failed command in windows command prompt (cmd.exe)

我试图将stderr重定向到stdout,然后使用tee在sceen上显示它并转储到文件。

当运行带有错字的命令时(cmd与'CommandNameWithATypo' is not recognized as an internal or external command, operable program or batch file.反应的命令'CommandNameWithATypo' is not recognized as an internal or external command, operable program or batch file. )我希望此错误消息在屏幕上显示并写入指定的文件在tee命令中。 但是,运行此命令:

CommandNameWithATypo 2>&1 | tee test.txt

我在屏幕上也没有输出到文件,也没有输出到文件(没有创建文件),好像管道从未发生过。 打算这样吗? 可能有解决方法吗?

问题是,“ cmd最了解重定向的位置” /为证明,使用>nul echo hello (不要关闭echo off )编写一个批处理文件,运行它并查看输出。 您可以使用代码块强制其将重定向绑定到代码的预期部分:

( CommandNameWithATypo 2>&1 ) | tee test.txt 

(对不起“您确实安装了tee,不是吗?”,但是如果没有tee ,您得到的正是您所描述的内容:什么都没有。请尝试使用wrongCommand 2>&1 | notTee file.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM