本文共 1062 字,大约阅读时间需要 3 分钟。
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 is okay like
让tee在stderr和上侦听的暴力方法是可以的
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/bashecho hello1>&2 echo world
It works as expected:
它按预期工作:
$ ./t.sh helloworld$ ./t.sh >/dev/nullworld$ ./t.sh 2>/dev/nullhello
Now, let’s redirect the STDERR to tee
:
现在,让我们将STDERR重定向到tee
:
$ ./t.sh 2> >(tee /tmp/log)hello$ worldcat /tmp/log world
tee
prints to STDOUT by . Let’s make it to STDERR:
tee
打印到STDOUT。 让我们将其到STDERR:
$ ./t.sh 2> >(tee /tmp/log >&2)helloworld$ cat /tmp/log world
Verify it:
验证一下:
$ (./t.sh 2> >(tee /tmp/log >&2) ) >/dev/nullworld
The “world” is not to the STDOUT anymore.
“世界”不再属于STDOUT。
翻译自:
tee stderr
转载地址:http://mulwd.baihongyu.com/