bash echo 到 stderr 和 tee 到日志文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13359276/
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-18 03:46:08  来源:igfitidea点击:

echo to stderr and tee to log file?

bash

提问by timy

In bash script,

在 bash 脚本中,

echo "error" 1>&2 | tee -a log

回声“错误” 1>&2 | tee - 一个日志

will print stderr in screen but no log to file, how to do these at same time?

将在屏幕上打印 stderr 但没有日志到文件,如何同时执行这些操作?

采纳答案by ams

To echo the text to both the log file and stderr, but not stdout, try this:

要将文本回显到日志文件和 stderr,而不是 stdout,请尝试以下操作:

echo "error" | tee -a log 1>&2

回答by P.P

echo "error" 1>&2 | tee -a log

With the first part 1>&2, what you are saying is: "Redirect stdout to stderr". So the echoedoutput "error"goes to stderr.

对于第一部分1>&2,您要说的是:“将标准输出重定向到标准错误”。所以回显的输出"error"进入 stderr。

Pipe (|) only reads from stdout, not stderr. So teedoesn't get any stdinat all from the pipe. Hence, it appends nothing to the log file.

Pipe( |) 只能从 读取stdout,不能从 读取stderr。所以tee根本没有stdin从管道中得到任何东西。因此,它不会在日志文件中附加任何内容。

回答by dogbane

To view both stdout and stderr on the console and send both streams to a log, redirect stderr to stdout as shown below:

要在控制台上查看 stdout 和 stderr 并将两个流发送到日志,请将 stderr 重定向到 stdout,如下所示:

progam.sh 2>&1 | tee -a log

回答by Benjamin Bannier

But default only stdout is passed along in pipes, so that in

但是默认情况下只有标准输出在管道中传递,因此在

$ echo "error" | tee 

teeonly sees the stout from the echo, not the stderr. stderr will still be displayed in the terminal.

tee只能从 中看到粗壮echo,而不是标准错误。stderr 仍将显示在终端中。