将bash stdout/stderr重定向到两个地方?

obe*_*tie 33 bash redirect

这个问题一直困扰着我一段时间.是否有可能重定向stdoutstderr到两个端子输出一个程序?

我知道可以将输出重定向到文件和stdoutwith tee,但我希望它转到程序(我的编辑器[TextMate])以及终端输出......当然这是可能的(我知道它可能与zsh ...)

Jas*_*ith 31

您可以使用命名管道,该管道专门用于您描述的情况.

mkfifo some_pipe
command_that_writes_to_stdout | tee some_pipe \
  & command_that_reads_from_stdin < some_pipe
rm some_pipe
Run Code Online (Sandbox Code Playgroud)

或者,在Bash中:

command_that_writes_to_stdout | tee >(command_that_reads_from_stdin)
Run Code Online (Sandbox Code Playgroud)

  • Bash的进程替换将在一步中为您完成此操作(类似于"匿名命名管道")command_that_writes_to_stdout | tee>(command_that_reads_from_stdin) (3认同)
  • @Nils:如果将`|`替换为`|&`,则bash会同时重定向两个stdout / stderr。 (2认同)

CB *_*ley 20

是否可以将stdout和stderr重定向到终端输出和程序?

我不确定将stdout和stderr组合在一个编辑器的输入上是多么有用,但这样的东西能做你需要的吗?

input_prog 2>&1 | tee /dev/tty | my_editor
Run Code Online (Sandbox Code Playgroud)

  • 至少在Linux上,您还可以使用`/ dev/stdout`和`/ dev/stderr` (2认同)