我如何可以重定向STDERR到STDOUT,但忽略原始STDOUT? [英] How can I redirect STDERR to STDOUT, but ignore the original STDOUT?
问题描述
我的 STDERR
输出我想检查和运行的grep
对等程序
I have a program whose STDERR
output I want to inspect and run grep
on etc.
所以,我可以把它重定向到 STDOUT
和使用grep,但问题是,我做的不的希望原始 STDOUT
的内容。
So I could redirect it to STDOUT
and use grep, but the problem is, I do not want the original STDOUT
content.
那么,这个人不会做
cmd 2>&1 | grep pattern
因为它将将原始输出和错误。
because it will mix the original STDOUT and STDERR.
和grep的,因为不读STDERR输出这一项不起作用:
And this one doesn't work since grep doesn't read the STDERR output:
cmd 1>/dev/null | grep pattern
但同时,这也不起作用:
But also, this one won't work:
cmd 1>/dev/null 2>&1 | grep pattern
因为输出会完全空的,因为一切都被写入的/ dev / null的
。
但是,必须有一个简单的方法来做到这一点?
But there must be a simple way to do it?
推荐答案
原因你引用了最后的命令:
What does not work:
The reason the last command you quoted:
cmd 1>/dev/null 2>&1 | grep pattern
不起作用,从其中重定向工作级的混乱造成的。你预期最后引用重定向被面前的每个输出施加到的那些,使得输出原来的标准输出文件描述符(1)将去到/ dev / null,并且输出到标准错误文件描述符(2)将到原标准输出。
does not work, stems from a confusion on the order in which redirection works. You expected the last quoted redirection to be applied to the ones before it on every output, so that output the original standard output file descriptor (1) will go to /dev/null, and output to the standard error file descriptor (2) will go to the original standard output.
然而,这不是重定向外壳是如何工作的。每个重定向通过关闭源和复制目标进去(见的
和人
网页导致文件描述符被重新映射 DUP(2)关闭(2)
),为了。这意味着,在您的命令标准输出首先用的/ dev / null的
替换,然后用标准的输出,这是取代标准误差的/ dev /空
了。
However, this is not how shell redirection works. Each redirection causes the file descriptors to be "remapped" by closing the "source" and duplicating the "destination" into it (see the man
pages of dup(2)
and close(2)
), in order. This means that in your command standard output is first replaced with /dev/null
, and then standard error replaced with standard output, which is /dev/null
already.
因此,为了获得预期的效果,你只需要扭转重定向。然后,你将有标准错误去到标准输出,和原来的标准输出转到的/ dev / null的
:
Therefore, to obtain the desired effect, you just need to reverse the redirections. Then you will have standard error go to standard output, and the original standard output go to /dev/null
:
cmd 2>&1 >/dev/null | grep pattern
(请注意, 1
在>
是不必要的 - 输出重定向标准输出是缺省值)
(note that the 1
before >
is unnecessary - for output redirection standard output is the default)
的附录的:查理提到将重定向到&安培; -
来关闭文件描述符。如果使用它支持的扩展名(庆典
和其他一些实现做但不是所有的交互式外壳,它是的不达标),你也可以做这样的:
Addendum: Charlie mentioned redirecting to &-
to close a file descriptor. If using an interactive shell which supports that extension (bash
and some other implementations do but not all and it is not standard), you can also do it like this:
cmd 2>&1 >&- | grep pattern
这可能会更好 - 它可以节省一些时间,因为当命令试图写入标准输出调用写
可能立即失败而无需等待的上下文切换到内核和驱动程序处理的/ dev / null的
(取决于系统调用实现 - 有些人可能在赶这个的libc
功能,有的还可能有的/ dev / null的
特殊处理)。如果有大量的输出,可以是有价值的,而且它的速度更快键入
This may be better - it can save some time, because when the command tries to write to standard output the call to write
may fail immediately without waiting for a context switch into the kernel and the driver handling /dev/null
(depending on the system call implementation - some may catch this in the libc
function, and some may also have special handling for /dev/null
). If there is a lot of output that can be worthwhile, and it's faster to type.
这将主要工作,因为如果他们不能写到标准输出大多数程序不在乎(谁真正检查返回值的printf
?),并不会介意标准输出关闭。但有些程序可以与失败code如果写摆脱困境
失败 - 通常是阻止处理器,使用一些仔细的库I / O或登录到stdandard输出的程序。所以,如果它不工作,请记住,这是一个可能的原因,并尝试的/ dev / null的
。
This will mostly work because most programs do not care if they fail to write to standard output (who really checks the return value of printf
?) and will not mind that standard output is closed. But some programs can bail out with a failure code if write
fails - usually block processors, programs using some careful library for I/O or logging to stdandard output. So if it doesn't work remember that this is a likely cause and try /dev/null
.
这篇关于我如何可以重定向STDERR到STDOUT,但忽略原始STDOUT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!