tee stderr_如何使Tee仅在Linux中捕获stderr?

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。

Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。

翻译自: https://www.systutorials.com/how-to-make-tee-catch-the-stderr-only-in-linux/

tee stderr

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值