tee stderr
I would like to make tee catch the stderr and log it into a file.
我想让tee捕获stderr并将其记录到文件中。
A brute force method by let tee listen on both stderr and stdout is okay like
让tee在stderr和stdout上侦听的暴力方法是可以的
cmd 2>&1 | tee -a log
But how to make tee catch the stderr only?
但是如何使T恤只接住stderr?
You can make use of “process substitution” (>(...)
) to creates a FIFO to catch the STDERR and pass it to tee
.
您可以使用“进程替换”( >(...)
)创建一个FIFO来捕获STDERR并将其传递给tee
。
Lets take an example t.sh:
让我们以t.sh为例:
#!/bin/bash
echo hello
1>&2 echo world
It works as expected:
它按预期工作:
$ ./t.sh
hello
world
$ ./t.sh >/dev/null
world
$ ./t.sh 2>/dev/null
hello
Now, let’s redirect the STDERR to tee
:
现在,让我们将STDERR重定向到tee
:
$ ./t.sh 2> >(tee /tmp/log)
hello
$ world
cat /tmp/log
world
tee
prints to STDOUT by default. Let’s make it print to STDERR:
tee
默认情况下打印到STDOUT。 让我们将其打印到STDERR:
$ ./t.sh 2> >(tee /tmp/log >&2)
hello
world
$ cat /tmp/log
world
Verify it:
验证一下:
$ (./t.sh 2> >(tee /tmp/log >&2) ) >/dev/null
world
The “world” is not to the STDOUT anymore.
“世界”不再属于STDOUT。
翻译自: https://www.systutorials.com/how-to-make-tee-catch-the-stderr-only-in-linux/
tee stderr