bash - 如何通过单独的 grep 过滤器发送 stderr 和 stdout?

标签 bash shell grep

<分区>

尝试运行一个产生大量标准输出和标准错误的命令。 需要一种方法来过滤大多数 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 的懒惰编码人员可能更喜欢像这样的更具体的答案。)

区分 stdoutstderr 的有用工具是 annotate-output , (安装 "devscripts" package ),它将 stderrstdin 连同有用的小前缀一起发送到 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

可以使用 sedawk 甚至 tee 和一些 grep 单独解析该输出

关于bash - 如何通过单独的 grep 过滤器发送 stderr 和 stdout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48728356/

相关文章:

bash - 在虚拟机中运行脚本,但从虚拟机外部调用

bash - 如何让 Emacs 使用我的 .bashrc 文件?

shell - 如何使用mailx以文本形式发送文件并向电子邮件正文添加额外文本?

git - 如何找到所有 .git 未经许可被拒绝的消息?

bash - 如何在 bash 脚本中使用远程命令结果分配局部变量?

linux - Bash 脚本不稳定的结果

bash - Bash脚本通过SSH远程传递命令

grep - 需要有关另一个 grep 命令输出的 grep 帮助

bash - 如何在 Unix 上查找所有文件并用逗号分隔结果?

超时运行的 Bash 脚本不会在 SIGINT 上退出