bash 如何开球到标准错误?

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

How to tee to stderr?

bashstderrtee

提问by djechlin

I want to split stdoutso that it is printed both to stdoutand stderr. This sounds like a job for teebut the syntax is evading me -

我想拆分stdout以便它同时打印到stdoutstderr。这听起来像是一份工作,tee但语法让我望而却步-

./script.sh | tee stderr

Of course, how should stderractually be referred to here?

当然,stderr这里究竟应该怎么称呼呢?

回答by brianegge

The only cross platform method I found which works in both interactive and non-interactive shells is:

我发现的唯一在交互式和非交互式 shell 中都有效的跨平台方法是:

command | tee >(cat 1>&2)

The argument to tee is a file or file handle. Using process substitutionwe send the output to a process. In the process =cat=, we redirect stdout to stderr. The shell (bash/ksh) is responsible for setting up the 1 and 2 file descriptors.

tee 的参数是文件或文件句柄。使用进程替换,我们将输出发送到进程。在进程=cat= 中,我们将stdout 重定向到stderr。shell (bash/ksh) 负责设置 1 和 2 文件描述符。

回答by Nicholas Wilson

./script.sh | tee /dev/fd/2

Note that this is dependant on OS support, not any built-in power in tee, so isn't universal (but will work on MacOS, Linux, Solaris, FreeBSD, probably others).

请注意,这取决于操作系统支持,而不是 tee 中的任何内置功能,因此不是通用的(但可以在 MacOS、Linux、Solaris、FreeBSD 上运行,可能还有其他)。

回答by David W.

./script.sh 2>&1 >/dev/null | tee stderr.out

That opens STDERR to STDOUT, and then disposes of STDOUT.

这将打开 STDERR 到 STDOUT,然后处理 STDOUT。