我想在 bash 脚本中提供一个可选的日志记录参数,并且想使用 exec
从一开始就tee
一个管道。但是,打开 tee
进程导致脚本挂起,我相信是因为 stdout 没有关闭:
# Output to a log file, if set
if [[ $OPT_LOG ]]; then
exec > >(tee -a $OPT_LOG)
fi
我试图结束:
exec >&-
但它仍然挂起 - 是否有另一种方法可以正确关闭 tee
以便脚本在执行结束时正确退出?
最佳答案
似乎出于某种原因,使用 tee
会阻止提示符 ($PS1
) 出现,因为 shell 脚本尚未退出。作为解决方法,我通常在调用 tee
之后使用短暂的 sleep
。
#!/bin/bash
exec > >(tee -a mylog)
sleep .1
# my code
关于linux - 使用 tee 时重定向输出挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30687504/