在 bash 脚本中间切换输出文件重定向

switch output file redirection in the middle of a bash script

我只想将脚本的输出保存在脚本开头我不知道的地方。我尝试了一些东西,但我很确定它很难看。 有没有一种优雅的方法可以做到这一点:

#!/bin/bash

# Here I don't know where to write outputfile so I create a tmpfile
fic=$(mktemp -t)
trap 'rm -f $fic' EXIT
rm -f $fic
:> $fic
exec 3<> $fic
exec 1>&3
exec 2>&3

# some code in particular reading options and dest dir
echo foo
dir="."

# Here I finally know where I can write my output
fic2=$dir/log.log

cp -f $fic $fic2
exec 3>&- # close fd #3
exec 1>> $fic2
exec 2>&1

echo bar

此外,我想 tee 整个输出,比如 $ exec ... >(tee $fic)$ 但我没有找到解决方案。

非常感谢您的任何建议。 PJLM

如果您知道两个输出文件都在同一个文件系统上,您可以只mv输出文件。您打开的文件描述符将继续工作。

exec 1>/tmp/out1 2>&1
echo out1
mv /tmp/out1 /tmp/out2   # replace with your desired destination
echo out2

如果你想 tee 输出,而且,同样,两个输出文件都在同一个文件系统上,你可以做几乎相同的事情(一旦 tee 打开了文件即使文件移动,写入它同样会继续写入同一个 fd。

log1=$(mktemp)
exec 3>"$log1"
exec 1> >(tee /dev/fd/3) 2>&1
echo out1
mv "$log1" "$log2"
echo out2

请注意,我没有执行 >(tee "$log1"),而是先在 shell 中打开 fd 3,然后使用 >(tee /dev/fd/3)。这是因为否则存在潜在的竞争条件,当我们到达 mv 步骤时 tee 将不会打开文件。 (exec 只等到 subshell 其中 tee 将 运行 开始,但是 tee 本身启动并打开文件)。


如果您的第一个和第二个 输出文件可能不在同一个文件系统上 ,您将必须进行一些更高级的改组并确保完成对第一个文件的写入在复制之前。

在简单重定向的情况下,我们需要在移动前关闭文件描述符:

exec 1>"$log1" 2>&1
echo out1
exec 1>&- 2>&-
mv "$log1" "$log2"
exec 1>>"$log2" 2>&1
echo out2

对于输出文件可能位于不同文件系统的进程替换,我们需要确保在移动文件之前完成进程替换:

exec 3>&1 4>&2                # save original stdout, stderr
exec 1> >(tee "$log1") 2>&1   # redirect to tee
pid=$!                        # save pid of tee's subshell

echo out1
exec 1>&3 2>&4                # restore original stdout, stderr

# wait until tee is done. on newer bash can use `wait $pid` instead
while kill -0 $pid 2>/dev/null; do :; done

mv "$log1" "$log2"

# repeat steps above for new file
exec 3>&1 4>&2
exec 1> >(tee -a "$log2") 2>&1
pid=$!
echo out2
exec 1>&3 2>&4
while kill -0 $pid 2>/dev/null; do :; done