gpt4 book ai didi

linux - 强制为 shell 脚本中的每个命令运行 `tee`?

转载 作者:IT王子 更新时间:2023-10-29 00:46:19 25 4
gpt4 key购买 nike

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

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

<command> | tee -a $LOGFILE

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

我不能强制用户在运行脚本 时添加适当的teeing,并希望确保即使调用用户也能正确记录不添加自己的日志调用。

最佳答案

您可以在脚本中做一个包装器:

#!/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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com