将 stdout 和 stderr 的副本重定向到一个文件,将 stderr 的副本重定向到 bash 脚本中的另一个文件
Redirect copy of stdout and stderr to one file, copy of just stderr to another file from within bash script
请注意,我已经查看了下面的两个 Whosebug 问题,但我的问题不同:
- redirect COPY of stdout to log file from within bash script itself
在尝试这样做时,我有以下 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)
这是一个有效的解决方案:
#!/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)
请注意,我已经查看了下面的两个 Whosebug 问题,但我的问题不同:
- redirect COPY of stdout to log file from within bash script itself
在尝试这样做时,我有以下 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)
这是一个有效的解决方案:
#!/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)