开发者

Output bash script into file (without >> )

开发者 https://www.devze.com 2023-02-14 05:08 出处:网络
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.

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 sc开发者_如何学Cript, 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号