Page 1 of 1

printf "[%lu]"

Posted: Fri Jul 22, 2016 7:09 am
by hymie
This is more of a curiouity than a problem.

Every script I see that is designed to use the Nagios command file uses a format like this:

NOW=`date +%s`
printf "[%lu] SCHEDULE_FORCED_SVC_CHECK;$1;serial_number;$NOW\n" $NOW > $COMMANDFILE

Note that the $NOW variable is used in two places -- once as the argument to replace %lu , and once inside the actual command string.

As far as I can see, both styles return the same value:

[1469189096] SCHEDULE_FORCED_SVC_CHECK;;serial_number;1469189096

So my question is, why do they need to be handled differently? Is there a specific reason why I can't say

echo "[$NOW] SCHEDULE_FORCED_SVC_CHECK;$1;serial_number;$NOW" > $COMMANDFILE

Re: printf "[%lu]"

Posted: Fri Jul 22, 2016 12:41 pm
by mcapra
hymie wrote: So my question is, why do they need to be handled differently? Is there a specific reason why I can't say

echo "[$NOW] SCHEDULE_FORCED_SVC_CHECK;$1;serial_number;$NOW" > $COMMANDFILE
There isn't a specific reason I can think of. It's all about what you prefer. printf is slightly safer in some situations.