Reputation: 77
I have a nightly report that is executed by cron with the following lines in it:
PRINTFX="/usr/bin/printf"
<snip>
${PRINTFX} "%-11s %-11s %'d\n" ${F1} ${F2} ${F3}
Crontab entry:
[ca-adm@homwpspect01 ~]$ crontab -l
55 01 * * * /usr/local/sbin/CRONSQLEvents
My Issue is that if I run this manually as root (i.e. with my login env settings), I get this [CORRECT] output:
Date Event Count
2013-02-19 0x00010802 516,616
2013-02-19 0x00010D66 351,840
2013-02-19 0x00010D67 351,533
When run by cron as another user, I get [InCorrect]:
Date Event Count
2013-02-19 0x00010802 516616
2013-02-19 0x00010D66 351840
2013-02-19 0x00010D67 351533
If it is run by cron, the comma is MISSING from the numeric output. I remember reading something about use of printf being ambiguous because there is a bash internal function, but there is also an external in /usr/bin:
[root@homwpspect01 sbin]# which printf
printf is a shell builtin
printf is /usr/bin/printf
Can someone tell me how to avoid this issue with cron versus logged in execution ? Especially given that I am specifying the /usr/bin/printf in the script anyway ?
Thanks in advance, Don
Upvotes: 4
Views: 406
Reputation: 77
So to get this to work correctly, I will add this line to the script:
export LANG=en_US.UTF-8
PRINTFX="/usr/bin/printf"
<snip>
${PRINTFX} "%-11s %-11s %'d\n" ${F1} ${F2} ${F3}
Upvotes: 0
Reputation: 263257
The format of a number printed with %'d
depends on the current locale, not (necessarily) on which printf
command you're using.
If your script is as you've shown us, you're invoking /usr/bin/printf
.
On my system:
$ echo $LANG
en_US.UTF-8
$ printf "%'d\n" 123456
123,456
$ /usr/bin/printf "%'d\n" 123456
123,456
$ LANG=C printf "%'d\n" 123456
123456
$ LANG=C /usr/bin/printf "%'d\n" 123456
123456
$
Bash's built-in printf
and /usr/bin/printf
(from GNU Coreutils) behave consistently.
You probably need to set $LANG
in your script to get the behavior you want.
The %'d
format is documented in the GNU libc manual:
'
Separate the digits into groups as specified by the locale specified for the
LC_NUMERIC
category; *note General Numeric::. This flag is a GNU extension.
Upvotes: 4