输出bash脚本到文件(无>>) [英] Output bash script into file (without >> )

查看:181
本文介绍了输出bash脚本到文件(无>>)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是很简单的事情了,但我不知道它。
我有cron中指定了作业,这在一段时间一次调用的脚本。

这样的事情: 1 1 * * *的bash script.sh

现在,脚本展开时,它读取配置和实现所有输出需要被附加到日志文件中。

简单的解决办法是更新如的cron任务1 1 * * *的bash script.sh>>日志
但让假设名称和文件的位置是不是在这个阶段闻名。

于是我想出了其中工程解决方案,但我不喜欢它了。
code。在脚本的顶部:

源config.sh

  N = $#
说明:last_arg = $ {!N}
如果[$说明:last_arg!=日志]
然后
  庆典\\`$基名0 \\'\\'回声$ @日志\\`>> $日志文件
  出口
科幻

我递归启动相同的脚本,额外的参数,方向日志文件。然后,家长例如刚刚关闭。

当然,必须有一个更好的方式来做到这一点?

感谢


解决方案

  EXEC 1 GT; /the/file/name.log

这将stdout重定向到/the/file/name.log

Perhaps this is very simple thing, but yet I dont know it. I have a job specified in cron, which calls a script once in a while.

something like that: 1 1 * * * bash script.sh

Now, when script launched it reads configuration and realises that all output needs to be appended to log file.

The simple solution would be to update cron task like 1 1 * * * bash script.sh >> log But let assume that name and location of the file is not known at this stage.

So I came up with the solution which works, but I dont like it a lot. Code at the top of the script:

source config.sh

N=$#
LAST_ARG=${!N}
if [ "$LAST_ARG" != "log" ]
then
  bash \`basename $0\` \`echo $@ log\` >>  "$LogFile"
  exit
fi

I recursively launching the same script, with additional argument, and direction to log file. Then Parent instance just getting closed.

Sure there has to be a better way to do it ?

Thanks

解决方案

exec 1>/the/file/name.log

This will redirect stdout to /the/file/name.log

这篇关于输出bash脚本到文件(无>>)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆