强制“tee”为 shell 脚本中的每个命令运行?
我想要一个脚本,其中所有命令都tee
到日志文件。
现在我正在运行脚本中的每个命令:
<command> | tee -a $LOGFILE
有没有办法强制 shell 脚本中的每个命令通过管道传输到 tee?
我无法强制用户在运行脚本时添加适当的tee
ing,并且希望确保它正确记录,即使调用用户不这样做添加自己的日志记录调用。
I would like to have a script wherein all commands are tee
'd to a log file.
Right now I am running every command in the script thusly:
<command> | tee -a $LOGFILE
Is there a way to force every command in a shell script to pipe to tee
?
I cannot force users to add appropriate tee
ing when running the script, and want to ensure it logs properly even if the calling user doesn't add a logging call of their own.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在脚本中进行包装:
编辑:
这是另一种方法:
You can do a wrapper inside your script:
Edit:
Here's another way:
为什么不公开一个简单的包装器:
您的用户不应该执行(甚至不知道)
yourOriginalScript.sh
。Why not expose a wrapper that's simply:
Your users should not execute (nor even know about)
yourOriginalScript.sh
.假设您的脚本不采用
--tee
参数,您可以执行此操作(如果您确实使用该参数,只需将下面的--tee
替换为您想要的参数即可不要使用):这只是让脚本重新运行本身,使用特殊参数
--tee
来防止无限递归,将其输出通过管道传输到tee
中。Assuming that your script doesn't take a
--tee
argument, you can do this (if you do use that argument, just replace--tee
below with an argument you don't use):This just has the script re-run itself, using the special argument
--tee
to prevent infinite recursion, piping its output intotee
.一些方法是创建运行程序脚本“run_it”,所有用户都调用自己的脚本。
所有的魔法都将在其中完成,例如看起来像这样:
Some approach would be creation of runner script "run_it" that all users invoke their own scripts.
All the magic would be done within, e.g. could look like that: