如何管标准错误,而不是标准输出? [英] How to pipe stderr, and not stdout?

查看:115
本文介绍了如何管标准错误,而不是标准输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有到标准输出标准错误写信息的程序,我需要的grep 通过什么来的标准错误的,而忽视的标准输出

I have a program that writes information to stdout and stderr, and I need to grep through what's coming to stderr, while disregarding stdout.

我当然可以做2个步骤:

I can of course do it in 2 steps:

command > /dev/null 2> temp.file
grep 'something' temp.file

但我想preFER才能够做到这一点没有临时文件。是否有任何智能管道花样?

but I would prefer to be able to do this without temp files. Are there any smart piping tricks?

推荐答案

首先将stderr重定向到标准输出 - 管道;然后重定向标准输出到的/ dev / null的(不改变的地方标准错误是怎么回事):

First redirect stderr to stdout — the pipe; then redirect stdout to /dev/null (without changing where stderr is going):

command 2>&1 >/dev/null | grep 'something'

有关在其所有的各种I / O重定向的详细信息,请参阅重定向章在Bash参考手册。

For the details of I/O redirection in all its variety, see the chapter on Redirections in the Bash reference manual.

请注意,I / O重定向的顺序是间preTED左到右,但管道设置了我前/ O重定向是相互preTED。文件描述符如1和2的参考打开文件的描述。操作 2 - ;&放大器; 1 使文件描述符2又名标准错误指的是同一个打开的文件描述,文件描述符1又名标准输出,目前指(见的 dup2() 和<一个href=\"http://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html#\"><$c$c>open()).操作&GT;的/ dev / null的然后更改文件描述符1,以便它指的是一个打开的文件描述了的/ dev / null的,但这并不改变文件描述符2指文件描述符1最初是指向打开文件描述的事实 - 即,管

Note that the sequence of I/O redirections is interpreted left-to-right, but pipes are set up before the I/O redirections are interpreted. File descriptors such as 1 and 2 are references to open file descriptions. The operation 2>&1 makes file descriptor 2 aka stderr refer to the same open file description as file descriptor 1 aka stdout is currently referring to (see dup2() and open()). The operation >/dev/null then changes file descriptor 1 so that it refers to an open file description for /dev/null, but that doesn't change the fact that file descriptor 2 refers to the open file description which file descriptor 1 was originally pointing to — namely, the pipe.

这篇关于如何管标准错误,而不是标准输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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