重定向标准输出到不同的日志一个COPY来自同一bash脚本中的文件多次 [英] Redirect a COPY of stdout to different log files multiple times from within same bash script

查看:192
本文介绍了重定向标准输出到不同的日志一个COPY来自同一bash脚本中的文件多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

行之后效果很好,当我们要重定向标准输出到一个文件,并同时获得印在标准输出。

Following line works well when we want to redirect the stdout to a file, and also get printed on stdout.

exec > >(tee logfile-1.txt)

但是,如果我们希望在以后的标准输出重定向到另一个文件,比方说,

However, if we wish to later redirect the stdout to another file, say,

exec > >(tee logfile-2.txt)

所面临的问题是,产量仍不断得到重定向到第二个文件(日志文件-2.txt)沿着第一个文件(日志文件-的1.txt)。

the issue faced is that the output still keeps getting redirected to the first file (logfile-1.txt) along with the second file (logfile-2.txt).

是否有相同的bash脚本包含的发言,并输出是排他性的每个对应于这些命令的执行顺序

Is there a way that the same bash script contains both the statement, and the output be exclusive to each of the files corresponding to the order of execution of these commands?

请注意:
这个问题是在扩展到解决方案以问的问题<一href=\"http://stackoverflow.com/questions/3173131/redirect-copy-of-stdout-to-log-file-from-within-bash-script-itself\">here.

推荐答案

我会做

{
    command1
    command2
} | tee logfile-1.txt

{
    command3
    command4
} | tee logfile-2.txt

此外,备份原来的文件描述符,第一:

Alternatively, backup the original filedescriptor, first:

exec 6<&1
exec > >(tee log1)
echo 1
echo 2
exec 1<&6 6<&-
exec > >(tee log2)
echo 3
echo 4

请参阅 http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/x13082.html 获取 EXEC

这篇关于重定向标准输出到不同的日志一个COPY来自同一bash脚本中的文件多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆