我想要一个脚本,其中所有命令都是 tee 到日志文件。

现在我正在运行脚本中的每个命令:

<command> | tee -a $LOGFILE

有没有办法强制 shell 脚本中的每个命令都通过管道传输到 tee

在运行 脚本 时,我无法强制用户添加适当的 tee ing,并且即使调用用户没有添加自己的日志记录调用,也希望确保它正确记录。

最佳答案

你可以在你的脚本中做一个包装:

#!/bin/bash
{
echo 'hello'
some_more_commands
echo 'goodbye'
} | tee -a /path/to/logfile

编辑:

这是另一种方式:
#!/bin/bash
exec > >(tee -a /path/to/logfile)
echo 'hello'
some_more_commands
echo 'goodbye'

关于linux - 强制 `tee` 为 shell 脚本中的每个命令运行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4037170/

10-13 08:52