【发布时间】:2023-03-09 20:01:02
【问题描述】:
尝试运行产生大量标准输出和标准错误的命令。 需要一种方法来过滤大多数 stderr 消息。
类似
command 1> grep "a" 2> grep "b"
另一种方法是只 grep stderr 但也看到 stdout。
【问题讨论】:
-
[此答案] (stackoverflow.com/a/31151808/9200529) 尤其全面且有用。
尝试运行产生大量标准输出和标准错误的命令。 需要一种方法来过滤大多数 stderr 消息。
类似
command 1> grep "a" 2> grep "b"
另一种方法是只 grep stderr 但也看到 stdout。
【问题讨论】:
做:
{ command 2>&1 1>&3 3>&- | grep "b"; } 3>&1 1>&2 | grep "a"
注意:以上内容改编自antak's answer to "pipe stdout and stderr to two different processes in shell script?",greps 替换了其更抽象的名称。有关它的工作原理,请参阅该答案。
(尽管如此,需要grep 的懒惰编码人员可能更喜欢这样的更具体的答案。)
用于区分 stdout 和 stderr 的有用工具是 annotate-output,(安装 "devscripts" package),它发送 stderr 和 stdin 和 stdout 以及有用的小前缀:
annotate-output wc -c /bin/bash /bin/nosuchshell
输出:
00:29:06 I: Started wc -c /bin/bash /bin/nosuchshell
00:29:06 E: wc: /bin/nosuchshell: No such file or directory
00:29:06 O: 1099016 /bin/bash
00:29:06 O: 1099016 total
00:29:06 I: Finished with exitcode 1
可以使用sed、awk,甚至是tee 和一些greps 单独解析该输出。
【讨论】: