【问题标题】:Redirect copy of stdout and stderr to one file, copy of just stderr to another file from within bash script将 stdout 和 stderr 的副本重定向到一个文件,将 stderr 的副本从 bash 脚本中重定向到另一个文件
【发布时间】:2016-03-29 11:27:34
【问题描述】:

请注意,我已经查看了以下两个 stackoverflow 问题,但我的问题有所不同:

在尝试执行此操作时,我有以下 test.sh 脚本:

#!/bin/bash

rm stdout_and_stderr.log
rm stderr.log

exec 2> >(tee -ia stderr.log >> stdout_and_stderr.log) 1> >(tee -ia stdout_and_stderr.log)

echo "stdout"
echo "stderr" >&2

唯一的问题是stderr没有显示到终端:

$ ./test.sh > /dev/null
$ ./test.sh 2> /dev/null
$ stdout

我的 bash 版本:

GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)

【问题讨论】:

  • 问题解决了吗?

标签: bash shell io-redirection


【解决方案1】:

这是一个可行的解决方案:

#!/bin/bash

rm stdout_and_stderr.log
rm stderr.log

exec 2> >(tee -ia stdout_and_stderr.log >&2)
exec 2> >(tee -ia stderr.log >&2)
exec 1> >(tee -ia stdout_and_stderr.log)

echo "stdout"
echo "stderr" >&2

也可以一行完成:

exec 1> >(tee -ia stdout_and_stderr.log) 2> >(tee -ia stdout_and_stderr.log >&2) 2> >(tee -ia stderr.log >&2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 2011-03-11
    • 2011-03-02
    • 2021-08-19
    相关资源
    最近更新 更多