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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/670784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:03:34  来源:igfitidea点击:

Redirecting bash stdout/stderr to two places?

bashredirect

提问by obeattie

This one's been bugging me for a while now. Is it possible to redirect stdoutand stderrto both the terminal output andto a program?

这个已经困扰我一段时间了。是否有可能重定向stdoutstderr到两个端子输出一个程序?

I understand it's possible to redirect the outputs to a file and to stdoutwith tee, but I want it to go to a program (my editor [TextMate]) as well as to the terminal output…?surely this is possible (I know its possible with zsh…)

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

采纳答案by JasonSmith

You can use a named pipe, which is intended for exactly the situation you describe.

您可以使用命名管道,它完全适用于您所描述的情况。

mkfifo some_pipe
command_that_writes_to_stdout | tee some_pipe \
  & command_that_reads_from_stdin < some_pipe
rm some_pipe

Or, in Bash:

或者,在 Bash 中:

command_that_writes_to_stdout | tee >(command_that_reads_from_stdin)

回答by CB Bailey

Is it possible to redirect stdout and stderr to both the terminal output and to a program?

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

I'm not sure how useful it is to combine stdout and stderr on the input to an editor, but does omething like this do what you need?

我不确定在编辑器的输入上结合 stdout 和 stderr 有多大用处,但是这样的事情是否可以满足您的需求?

input_prog 2>&1 | tee /dev/tty | my_editor

回答by paxdiablo

I don't actually know whether TextMate can take a file to edit as its standard input, that seems a little bizarre. I suspect you would want to send the stdout/stderr to a file and edit it there, in which case you need:

我实际上不知道 TextMate 是否可以将要编辑的文件作为其标准输入,这似乎有点奇怪。我怀疑你想将 stdout/stderr 发送到一个文件并在那里编辑它,在这种情况下你需要:

progname 2>&1 | tee tempfile ; textmate tempfile

The 2>&1redirects stderr(file handle 2) to go to the same place as stdout(file handle 1) so that both of them end up in a single stream. The teecommand then writes that to tempfileas well as stdout.

2>&1重定向stderr(文件句柄2)去同一个地方stdout(文件句柄1),以便在一个单一的数据流两者的结束。tee然后该命令将其写入tempfile以及stdout.

Then, once the process has finished, the editor is called up on the temporary file.

然后,一旦该过程完成,就会在临时文件上调用编辑器。

If it can accept standard input for editing, use:

如果它可以接受标准输入进行编辑,请使用:

progname 2>&1 | tee /dev/tty | textmate