我想要一个脚本,其中所有命令都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 - 强制为 shell 脚本中的每个命令运行 `tee`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4037170/