linux 管道写入文件,linux - 在管道上使用“tee”时如何将stderr写入文件? - SO中文参考 - www.soinside.com...

701

投票

我假设您仍希望在终端上看到STDERR和STDOUT。你可以去找Josh Kelley的答案,但我发现在后台保持一个tail,输出你的日志文件非常hackish和cludgy。注意你需要保留一个exra FD并在之后通过杀死它进行清理,技术上应该在trap '...' EXIT中这样做。

有一种更好的方法可以做到这一点,你已经发现了它:tee。

只是,而不是仅仅为stdout使用它,有stdout的发球台和stderr的发球台。你将如何实现这一目标?进程替换和文件重定向:

command > >(tee -a stdout.log) 2> >(tee -a stderr.log >&2)

让我们分开并解释一下:

> >(..)

>(...)(进程替换)创建一个FIFO,让tee听取它。然后,它使用>(文件重定向)将command的STDOUT重定向到您的第一个tee正在侦听的FIFO。

同样的事情是第二个:

2> >(tee -a stderr.log >&2)

我们再次使用进程替换来创建一个从STDIN读取并将其转储到tee的stderr.log进程。 tee在STDOUT上输出其输入,但由于它的输入是我们的STDERR,我们想要再次将tee的STDOUT重定向到我们的STDERR。然后我们使用文件重定向将command的STDERR重定向到FIFO的输入(tee的STDIN)。

过程替换是你选择bash作为你的shell而不是sh(POSIX或Bourne)的额外奖励之一。

在sh,你必须手动做事:

out="${TMPDIR:-/tmp}/out.$$" err="${TMPDIR:-/tmp}/err.$$"

mkfifo "$out" "$err"

trap 'rm "$out" "$err"' EXIT

tee -a stdout.log < "$out" &

tee -a stderr.log < "$err" >&2 &

command >"$out" 2>"$err"

593

投票

为什么不简单:

./aaa.sh 2>&1 | tee -a log

这简单地将stderr重定向到stdout,因此tee回应记录和屏幕。也许我错过了一些东西,因为其他一些解决方案看起来很复杂。

注意:从bash版本4开始,您可以使用|&作为2>&1 |的缩写:

./aaa.sh |& tee -a log

52

投票

这可能对通过谷歌找到这个的人有用。只需取消注释您想要尝试的示例即可。当然,随意重命名输出文件。

#!/bin/bash

STATUSFILE=x.out

LOGFILE=x.log

### All output to screen

### Do nothing, this is the default

### All Output to one file, nothing to the screen

#exec > ${LOGFILE} 2>&1

### All output to one file and all output to the screen

#exec > >(tee ${LOGFILE}) 2>&1

### All output to one file, STDOUT to the screen

#exec > >(tee -a ${LOGFILE}) 2> >(tee -a ${LOGFILE} >/dev/null)

### All output to one file, STDERR to the screen

### Note you need both of these lines for this to work

#exec 3>&1

#exec > >(tee -a ${LOGFILE} >/dev/null) 2> >(tee -a ${LOGFILE} >&3)

### STDOUT to STATUSFILE, stderr to LOGFILE, nothing to the screen

#exec > ${STATUSFILE} 2>${LOGFILE}

### STDOUT to STATUSFILE, stderr to LOGFILE and all output to the screen

#exec > >(tee ${STATUSFILE}) 2> >(tee ${LOGFILE} >&2)

### STDOUT to STATUSFILE and screen, STDERR to LOGFILE

#exec > >(tee ${STATUSFILE}) 2>${LOGFILE}

### STDOUT to STATUSFILE, STDERR to LOGFILE and screen

#exec > ${STATUSFILE} 2> >(tee ${LOGFILE} >&2)

echo "This is a test"

ls -l sdgshgswogswghthb_this_file_will_not_exist_so_we_get_output_to_stderr_aronkjegralhfaff

ls -l ${0}

22

投票

要将stderr重定向到文件,将stdout显示到屏幕,并将stdout保存到文件:

./aaa.sh 2>ccc.out | tee ./bbb.out

编辑:要显示stderr和stdout到屏幕并同时保存到文件,你可以使用bash的I/O redirection:

#!/bin/bash

# Create a new file descriptor 4, pointed at the file

# which will receive stderr.

exec 4<>ccc.out

# Also print the contents of this file to screen.

tail -f ccc.out &

# Run the command; tee stdout as normal, and send stderr

# to our file descriptor 4.

./aaa.sh 2>&4 | tee bbb.out

# Clean up: Close file descriptor 4 and kill tail -f.

exec 4>&-

kill %1

15

投票

换句话说,您希望将stdout传输到一个过滤器(tee bbb.out)和stderr到另一个过滤器(tee ccc.out)。没有标准方法可以将除stdout之外的任何内容传递给另一个命令,但是您可以通过处理文件描述符来解决这个问题。

{ { ./aaa.sh | tee bbb.out; } 2>&1 1>&3 | tee ccc.out; } 3>&1 1>&2

在bash(和ksh和zsh)中,但在其他POSIX shell(如dash)中没有,你可以使用process substitution:

./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out)

请注意,在bash中,只要./aaa.sh完成,该命令就会返回,即使tee命令仍然执行(ksh和zsh等待子进程)。如果你做像./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out); process_logs bbb.out ccc.out这样的事情,这可能是个问题。在这种情况下,请改用文件描述符juggling或ksh / zsh。

12

投票

如果使用bash:

# Redirect standard out and standard error separately

% cmd >stdout-redirect 2>stderr-redirect

# Redirect standard error and out together

% cmd >stdout-redirect 2>&1

# Merge standard error with standard out and pipe

% cmd 2>&1 |cmd2

2

投票

在我的例子中,脚本运行命令同时将stdout和stderr重定向到文件,如:

cmd > log 2>&1

我需要更新它,以便在出现故障时,根据错误消息采取一些措施。我当然可以删除dup 2>&1并从脚本中捕获stderr,但随后错误消息将不会进入日志文件以供参考。虽然@lhunath接受的答案应该是这样做的,但它会将stdout和stderr重定向到不同的文件,这不是我想要的,但它帮助我提出了我需要的确切解决方案:

(cmd 2> >(tee /dev/stderr)) > log

有了上面,日志将有stdout和stderr的副本,我可以从我的脚本捕获stderr,而不必担心stdout。

2

投票

以下内容适用于KornShell(ksh),其中进程替换不可用,

# create a combined(stdin and stdout) collector

exec 3 <> combined.log

# stream stderr instead of stdout to tee, while draining all stdout to the collector

./aaa.sh 2>&1 1>&3 | tee -a stderr.log 1>&3

# cleanup collector

exec 3>&-

这里真正的诀窍是2>&1 1>&3的序列,在我们的例子中,它将stderr重定向到stdout并将stdout重定向到描述符3。此时stderr和stdout尚未合并。

实际上,stderr(作为stdin)被传递到tee,在那里它记录到stderr.log并且还重定向到描述符3。

描述符3一直将它记录到combined.log。所以combined.log包含stdout和stderr。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值