简体   繁体   English

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

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

I'm trying to redirect stderr to stdout and then use tee to both show it on sceen and dump to a file. 我试图将stderr重定向到stdout,然后使用tee在sceen上显示它并转储到文件。

When running a command with a typo (to which cmd reacts with 'CommandNameWithATypo' is not recognized as an internal or external command, operable program or batch file. ) I expect this error message to be shown on screen and also written to the file specified in the tee command. 当运行带有错字的命令时(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命令中。 However, running this: 但是,运行此命令:

CommandNameWithATypo 2>&1 | tee test.txt

I get no output on screen nor to a file (no file is created), as if piping never happened. 我在屏幕上也没有输出到文件,也没有输出到文件(没有创建文件),好像管道从未发生过。 Is this intended to be like this? 打算这样吗? May there be a workaround? 可能有解决方法吗?

the problem is, that "cmd knows best where to put redirection" /for prove, write a batch file with >nul echo hello (don't turn echo off ), run it and watch the output. 问题是,“ cmd最了解重定向的位置” /为证明,使用>nul echo hello (不要关闭echo off )编写一个批处理文件,运行它并查看输出。 You can force it to bind redirection to the intended part of the code with a code block: 您可以使用代码块强制其将重定向绑定到代码的预期部分:

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

(sorry for "you do have tee installed, don't you?", but without tee , you get exactly what you described: nothing. Try wrongCommand 2>&1 | notTee file.txt ) (对不起“您确实安装了tee,不是吗?”,但是如果没有tee ,您得到的正是您所描述的内容:什么都没有。请尝试使用wrongCommand 2>&1 | notTee file.txt

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

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