printf "[%lu]"

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
hymie
Posts: 5
Joined: Fri Jan 29, 2016 10:51 am

printf "[%lu]"

Post 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
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: printf "[%lu]"

Post 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.
Former Nagios employee
https://www.mcapra.com/
Locked