管道在bash标准输出和标准错误? [英] Piping both stdout and stderr in bash?
问题描述
似乎庆典的新版本有&放大器;>
运算符,它(如果我理解正确的话),标准输出和标准错误重定向到一个文件(&放大器;>>
追加到文件,而不是作为阿德里安澄清)。
It seems that newer versions of bash have the &>
operator, which (if I understand correctly), redirects both stdout and stderr to a file (&>>
appends to the file instead, as Adrian clarified).
什么来达到同样的东西的最简单的方式,而是通过管道到另一命令?
What's the simplest way to achieve the same thing, but instead piping to another command?
例如,在这一行:
cmd-doesnt-respect-difference-between-stdout-and-stderr | grep -i SomeError
我想的grep能够同时在输出和错误匹配的内容(实际上,让他们合并成一个流)。
I'd like the grep to match on content both in stdout and stderr (effectively, have them combined into one stream).
的注意的:这个问题是问的管道,的不的重定向 - 因此它是不是它目前被标记为一个重复的问题重复
Note: this question is asking about piping, not redirecting - so it is not a duplicate of the question it's currently marked as a duplicate of.
推荐答案
(注意:&放大器;>>文件
的追加的到文件,而&放大器;方式>
会重定向和覆盖的一个previously现有的文件)
(Note that &>>file
appends to a file while &>
would redirect and overwrite a previously existing file.)
要结合标准输出
和标准错误
你会使用后者重定向到前者 2 - ;&放大器; 1
。这重定向标准错误(文件描述符2)到标准输出(文件描述符1),例如:
To combine stdout
and stderr
you would redirect the latter to the former using 2>&1
. This redirects stderr (file descriptor 2) to stdout (file descriptor 1), e.g.:
$ { echo "stdout"; echo "stderr" 1>&2; } | grep -v std
stderr
$
标准输出
将发送到stdout,标准错误
进入标准错误。 的grep
只能看到标准输出
,因此,标准错误
打印到终端
stdout
goes to stdout, stderr
goes to stderr. grep
only sees stdout
, hence stderr
prints to the terminal.
在另一方面:
$ { echo "stdout"; echo "stderr" 1>&2; } 2>&1 | grep -v std
$
写入标准输出和标准错误, 2 - 后;&放大器; 1
标准错误重定向到标准输出和的grep
看无论在标准输入字符串,从而过滤掉两个。
After writing to both stdout and stderr, 2>&1
redirects stderr back to stdout and grep
sees both strings on stdin, thus filters out both.
您可以阅读更多有关重定向这里。
You can read more about redirection here.
对于你的榜样(POSIX):
Regarding your example (POSIX):
cmd-doesnt-respect-difference-between-stdout-and-stderr 2>&1 | grep -i SomeError
或者使用> =庆典-4
:
cmd-doesnt-respect-difference-between-stdout-and-stderr |& grep -i SomeError
这篇关于管道在bash标准输出和标准错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!