只将标准输出导向到另一个命令,将标准错误重定向到终端的标准输出?

huangapple go评论103阅读模式
英文:

How can I only pipe the stdout to another command, and redirecting stderr to the standard out of the terminal?

问题

我有一个名为 `foo` 的命令,我想将其 `stdout` 管道传输到另一个名为 `bar` 的命令,其输出我想要管道传输到 `bas`。如果 `foo` 失败,我希望在控制台的默认标准输出中看到它。最佳方法是什么?

对于澄清,如果 `foo` 以 stderr 输出失败,我希望直接将其作为结果看到,而不是被 `bar``bas` 处理。

$ foo | bar | bas
This is error written to stderr by foo.
英文:

I have a command foo whose stdout I want to pipe to another command bar, whose output I want to pipe to bas. If foo fails, I want to see it in the default standard out of the console. What is the best approach?

$ foo | bar | bas

For clarification, if foo fails with output in the stderr, I want to see it directly as a result, and not being processed by bar or bas.

$ foo | bar | bas
This is error written to stderr by foo.

答案1

得分: 2

确保 "foo" 不在内部将 stderr 重定向到 stdout。

默认情况下,stderr 不会被路由到管道。

只有在以下情况下才会 "丢失" stderr 流:

foo 2>&1 | bar | bas

或者

foo |& bar | bas
英文:

Make sure that "foo" doesn't redirect stderr to stdout internally.

By default stderr is NOT routed to pipe.

You would only "lose" that stderr stream if you did either

foo 2>&1 | bar | bas

or

foo |& bar | bas

huangapple
  • 本文由 发表于 2023年2月19日 13:18:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定