有没有一种方法,只标准错误重定向到标准输出(而不是将二者结合起来),因此它可以被输送到其它程序? [英] Is there a way to redirect ONLY stderr to stdout (not combine the two) so it can be piped to other programs?

查看:138
本文介绍了有没有一种方法,只标准错误重定向到标准输出(而不是将二者结合起来),因此它可以被输送到其它程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows工作 CMD.EXE 环境,并想改变标准输出的输出匹配标准错误,这样我可以通过管道将错误信息发送到其他程序没有文件的中介。

I'm working in a Windows CMD.EXE environment and would like to change the output of stdout to match that of stderr so that I can pipe error messages to other programs without the intermediary of a file.

我知道了的 2 - ;&放大器; 1 符号,但它结合了标准输出标准错误成一个单一的数据流。

I'm aware of the 2>&1 notation, but that combines stdout and stderr into a single stream.

我在想什么会是这样的:

What I'm thinking of would be something like this:

program.exe 2>&1 | find " "

不过,结合输出和错误,就像:

But that combines stdout and stderr just like:

program.exe | find " " 2>&1

我知道我能做到......

I realize that I could do...

program 2>file
type file | find " "
del file

但是,这并不有一个程序的灵活性和功能|发现,排序记号。这样做要求程序已完成其输出之前可以处理的输出。

But this does not have the flexibility and power of a program | find " " sort of notation. Doing this requires that program has finished with its output before that output can be processed.

推荐答案

有趣的问题: - )

CMD处理重定向从左到右。你想先重定向2(标准错误)到&安培; 1(标准输出),然后重定向1(标准输出)到别的东西。在这一点上标准错误仍将被重定向到标准输出的previous定义。管道仍然可以与标准输出的旧的定义工作(现在包含标准错误)。

CMD processes redirection from left to right. You want to first redirect 2 (stderr) to &1 (stdout), then redirect 1 (stdout) to something else. At this point stderr will still be redirected to the previous definition of stdout. The pipe will still work with the old definition of stdout (which now contains stderr).

如果你不关心标准输出,那么你可以重定向到NUL

If you don't care about stdout then you can redirect to nul

program.exe 2>&1 1>nul | find " "


如果你想捕捉标准输出到文件,然后重定向到一个文件


If you want to capture stdout to a file then redirect to a file

program.exe 2>&1 1>yourFile | find " "


如果您仍然希望在控制台上看到标准输出,但你只想管标准错误找,那么你就可以重定向1 CON:


If you still want to see stdout on the console, but you only want to pipe stderr to FIND, then you can redirect 1 to con:

program.exe 2>&1 1>con: | find " "

请注意,有标准输出和反对的原始定义之间有微妙的区别:。例如, CLS> CON:不清除屏幕,它打印一个有趣的字符在屏幕上,而不是

Note that there is a subtle difference between the original definition of stdout and con:. For example, cls >con: does not clear the screen, it prints a funny character to the screen instead.

这是可能的,如果您使用的是3日(最初未使用)的文件句柄真正交换输出和错误。 1和3将包含标准错误的原始定义,和2将包含标准输出的原始定义。

It is possible to truly swap stdout and stderr if you use a 3rd (initially unused) file handle. 1 and 3 will contain original definition of stderr, and 2 will contain original definition of stdout.

program.exe 3>&2 2>&1 1>&3 | find " "

其实没有定义执行重定向每次额外的文件句柄。原始定义被保存在第一个可用的未使用的文件句柄。假设没有发生过之前发出上述命令的重定向。 3 GT;和2 不保存3的原始定义,因为3不是pviously定义$ P $。但 2 - ;&放大器; 1 节约标准错误4的原始定义(3已经被使用),而 1>和2 保存在5标准输出的原始定义。

Actually there is an additional file handle defined every time a redirection is performed. The original definition is saved in the first available unused file handle. Assume there has not been any redirection prior to issuing the above command. 3>&2 does not save the original definition of 3 because 3 was not previously defined. But 2>&1 saves the original definition of stderr in 4 (3 has already been used), and 1>&2 saves the original definition of stdout in 5.

所以从技术上来说,是不需要的3明确重定向交换标准错误和标准输出

So technically, the explicit redirection of 3 is not needed to swap stderr and stdout

program.exe 2>&1 1>&3 | find " "

2 - ;&放大器; 1 保存标准错误在3和2被重定向到&安培; 1(标准输出)。 1 GT;及3 4和1个标准输出保存被重定向到和3(标准错误)

2>&1 saves stderr in 3 and 2 is redirected to &1 (stdout). 1>&3 saves stdout in 4 and 1 is redirected to &3 (stderr).

但上面的,如果你是积极的,3尚未之前发出的命令定义将只正常工作。它是更安全的明确定义3在我之前code的例子。

But the above will only work properly if you are positive that 3 has not already been defined prior to issuing the command. It is much safer to explicitly define 3 as in my prior code example.

请参阅How永久重定向再度标准错误返回到控制台了解一些非常疯狂的冒险与重定向: - ?)

See How to permanently redirect standard error back to the console again? for some really wild adventures with redirection :-)

这篇关于有没有一种方法,只标准错误重定向到标准输出(而不是将二者结合起来),因此它可以被输送到其它程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆