Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ksh arithmetic has problems with leading zeroes?

254 views
Skip to first unread message

Bob Menschel

unread,
May 6, 2002, 9:59:36 PM5/6/02
to
AIX 5.1, shell = ksh, "$(( $a + $b ))" is supposed to add $a and $b. It
usually does. Examples:

> inmm=$(( 007 + 1)) ; echo $inmm
8
> inmm=$(( 9 + 1)) ; echo $inmm
10
> inmm=$(( 07 + 1)) ; echo $inmm
8
> inmm=$(( 06 + 1)) ; echo $inmm
7

You'll note that several of the numbers used above have leading zeroes.
This is common when doing arithmetic with months (eg: get today's date, add
one to the month).

Unfortunately, ksh doesn't seem to be able to handle these leading zeroes in
all cases. Specifically, it appears from our experimentation that leading
zeroes in front of an 8 or 9 cause an error:

> inmm=$(( 06 + 09)) ; echo $inmm
ksh: 09: 0403-009 The specified number is not valid for this command.
> inmm=$(( 008 + 1)) ; echo $inmm
ksh: 008 + 1: 0403-009 The specified number is not valid for this command.
> inmm=$(( 009 + 1)) ; echo $inmm
ksh: 009 + 1: 0403-009 The specified number is not valid for this command.
> inmm=$(( 09 + 1)) ; echo $inmm
ksh: 09 + 1: 0403-009 The specified number is not valid for this command.

We discovered this today -- before I report it to IBM, I'd like to know
whether this is a common situation or whether it's unique to our system. Do
other peoples' systems exhibit the same kind of inconsistency?

Bob Menschel


Paul Landay

unread,
May 7, 2002, 5:50:36 AM5/7/02
to Bob Menschel
Bob Menschel wrote:
:

> Unfortunately, ksh doesn't seem to be able to handle these leading zeroes in
> all cases. Specifically, it appears from our experimentation that leading
> zeroes in front of an 8 or 9 cause an error:
:


The leading zero indicates an octal number,
and of course 08 and 09 are invalid octal numbers.

Paul Landay

Urban A. Haas

unread,
May 7, 2002, 11:54:07 AM5/7/02
to

"Paul Landay" <lan...@us.ibm.com> wrote in message
news:3CD7A36C...@us.ibm.com...

If you modify your KSH program to use the let built-in ksh statement, it
should correct the problem.

Paul's post is correct.

Urban


Bob Menschel

unread,
May 8, 2002, 1:44:03 AM5/8/02
to
Paul/Urban,

Thanks. After reading Paul's response I did a little reading (this behavior
is different from the systems I'm used to -- Sequent PTX and SCO Unix, at
least the ksh shells we've used, don't have this automatic octal behavior --
they treat all numbers within (( ... )) as decimal integers unless something
specifically tells them to do octal or other base work).

From that reading, I gathered that the LET command would have the same octal
behavior, no?

Instead, since everything said that if base is not specified, the first use
of a variable sets the base, if I initialize my ksh script with something
like var=9, then the sequence
> mm=getmonth() # returns 2-digit month number
> nextmo=$(( $mm + 1 ))
should do decimal arithmetic rather than octal for months 01-09, yes?

Bob


"Urban A. Haas" <uh...@urbantechnology.com> wrote in message
news:3cd7fb05$1...@nntp01.splitrock.net...

Paul Landay

unread,
May 8, 2002, 6:37:52 AM5/8/02
to Bob Menschel
Bob Menschel wrote:
>
> Paul/Urban,
>
> Thanks. After reading Paul's response I did a little reading (this behavior
> is different from the systems I'm used to -- Sequent PTX and SCO Unix, at
> least the ksh shells we've used, don't have this automatic octal behavior --
> they treat all numbers within (( ... )) as decimal integers unless something
> specifically tells them to do octal or other base work).
>
> From that reading, I gathered that the LET command would have the same octal
> behavior, no?
>
> Instead, since everything said that if base is not specified, the first use
> of a variable sets the base, if I initialize my ksh script with something
> like var=9, then the sequence
> > mm=getmonth() # returns 2-digit month number
> > nextmo=$(( $mm + 1 ))
> should do decimal arithmetic rather than octal for months 01-09, yes?

The documentation you have been reading is probably for ksh93.
The default ksh shipped with AIX 5.1 is still the 88 (?) level,
but ksh93 is shipped with AIX 5.1:
ls -l /bin/ksh*
Change the first line to:
#!/bin/ksh93
to use the ksh93 version.

It looks to me like 'let' on the old ksh has the same
octal notation issue as without 'let', but either way
ksh93 does what you want.

Paul Landay

0 new messages