我有一个程序,将信息写入标准输出和标准错误,我需要用grep处理标准错误,把标准错误放在一边。
使用临时文件,可以分两步完成:
command > /dev/null 2> temp.file
grep 'something' temp.file
但是,在没有临时文件的情况下,如何使用一个命令和管道实现这一点呢?
我有一个程序,将信息写入标准输出和标准错误,我需要用grep处理标准错误,把标准错误放在一边。
使用临时文件,可以分两步完成:
command > /dev/null 2> temp.file
grep 'something' temp.file
但是,在没有临时文件的情况下,如何使用一个命令和管道实现这一点呢?
当前回答
在Bash中,你也可以使用进程替换重定向到子shell:
command > >(stdout pipe) 2> >(stderr pipe)
对于手头的案子:
command 2> >(grep 'something') >/dev/null
其他回答
如果你正在使用Bash,那么使用:
command >/dev/null |& grep "something"
http://www.gnu.org/software/bash/manual/bashref.html#Pipelines
您可以使用rc外壳。
首先安装包(小于1mb)。
这是一个如何在rc中丢弃标准输出和管道标准错误到grep的例子:
find /proc/ >[1] /dev/null |[2] grep task
你可以在不离开Bash的情况下完成:
rc -c 'find /proc/ >[1] /dev/null |[2] grep task'
您可能已经注意到,可以通过在管道后面使用括号来指定想要管道的文件描述符。
标准文件描述符的编号如下:
0:标准输入 1:标准输出 2:标准错误
我试着跟着做,发现它也有用,
command > /dev/null 2>&1 | grep 'something'
我刚刚提出了一个解决方案,使用命名管道将stdout发送到一个命令,stderr发送到另一个命令。
在这里。
mkfifo stdout-target
mkfifo stderr-target
cat < stdout-target | command-for-stdout &
cat < stderr-target | command-for-stderr &
main-command 1>stdout-target 2>stderr-target
之后删除已命名的管道可能是个好主意。
在Bash中,你也可以使用进程替换重定向到子shell:
command > >(stdout pipe) 2> >(stderr pipe)
对于手头的案子:
command 2> >(grep 'something') >/dev/null