1834091 Members
2032 Online
110063 Solutions
New Discussion

Re: Printf bug?

 
SOLVED
Go to solution
Kris Spander
Advisor

Printf bug?

Hello,

I am having a problem with using printf in a script.

HR=08
printf "Hour: %02d\n" $HR

printf: Error converting 08

It works for 00 01 02 03 04 05 06 07 10 11 12. Is this a bug that I need to report to HP?

Thanks, Kris
Dilbert is my hero.
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Printf bug?

Not really. The shell is seeing a leading zero and is trying to intepret the value as an octal value. 08 and 09 are invalid.

Change
HR=08
to typeset -i HR=08
and all will be well.
If it ain't broke, I can fix that.
Gregory Fruth
Esteemed Contributor

Re: Printf bug?

printf() appears to be converting your input
string "08" to an integer using the usual C
language conventions; that is, a leading "0x"
means the number is in hexadecimal, and a
leading "0" means the number is in octal.
"8" is not a valid digit in an octal number.

You'll need to either strip off the leading 0
or use "typeset -i" to make ksh treat HR
as an integer.

typeset -i HR
HR=08
printf "Hour: %02d\n" $HR

Hour: 08
Martin Robinson
Frequent Advisor

Re: Printf bug?

I got bitten by this in January when the month changed from 12 to 01.

There is a difference between /bin/sh and /usr/bin/ksh. I had to change the first line of my script from
#!/bin/sh
to
#!/usr/bin/ksh

I was doing
now=`date +%m%d`
time=$((now - 1))
and /bin/sh gives the wrong results.


Dietmar Konermann
Honored Contributor

Re: Printf bug?

With recent patches the POSIX shell (/sbin/sh and /usr/sbin/sh) was changed to comply with latest POSIX standards. So, what you are seeing is designed behaviour.

For details see e.g. patch text of PHCO_25597 (JAGad93413 and its special installation instructions).

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)