Operating System - HP-UX
1830938 Members
2115 Online
110017 Solutions
New Discussion

Re: Shell error with addition

 
SOLVED
Go to solution
marc seguin
Regular Advisor

Shell error with addition

With /usr/bin/sh on HP-UX 11.11, i get this strange error :

# typeset -Z2 numero=7
# echo $numero
07
# (( numero = $numero + 1 ))
# echo $numero
08
# (( numero = $numero + 1 ))
sh: 08 + 1 : The specified number is not valid for this command.

Is there a patch for this bug ?
9 REPLIES 9
Jeff Schussele
Honored Contributor

Re: Shell error with addition

Hi Marc,

This has to be due to the -Z.
Try it with -i2 - that defines it as an integer & speeds the math to boot.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
john korterman
Honored Contributor

Re: Shell error with addition

Hi,
I don't that it is even oficailly recognized as a bug: a numeric string preceded with 0 is interpreted as octal notation. You probaly have to change your variable definition to something like
typeset -i numero=7
and then perform other operations in order to write it with a zero in front.

regards,
John K.
it would be nice if you always got a second chance
Jeff Schussele
Honored Contributor

Re: Shell error with addition

Hi John,

The -Z right justifies & pads the value with leading zeros if the first nonblank char is a digit. The pad depends on the n value - in this case 2.
So the 7 is padded to 07.

BUT - I don't *think* it's stored as an integer & I think that's what is causing the "message".

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
john korterman
Honored Contributor

Re: Shell error with addition

Hi Jeff,
you are probably right. I just thought that the system was protesting against doing an addition to what it considered being the highest octal number.

regards,
John K.
it would be nice if you always got a second chance
marc seguin
Regular Advisor

Re: Shell error with addition

I don't agree with your explanations.
If my parameter is to be considered as an octal value, it can't be 8 (or 08). And "07 + 1" would give 10.

With HP-UX 10.20, i don't have this result.
I get :
# typeset -Z2 string=7
# (( string = $string + 1 ))
# echo $string
08
# (( string = $string + 1 ))
# echo $string
09

Which is OK !!

With HP-UX 11.11, i can solve my problem in using two parameters ,one as a string[Z2] and one as an integer. But i don't think it is a normal behaviour.
john korterman
Honored Contributor
Solution

Re: Shell error with addition

Hi again Marc,
I still think the problem is related to the system perceiving "08" as an octal value.
And although I do not like to disagree with Pharaohs I think the problem fits the description for patch PHCO_29699, which holds a reference to patch PHCO_26789:
http://www4.itrc.hp.com/service/cki/patchDocDisplay.do?patchId=PHCO_29699

Please see the "Defect description" and the error message.

regards,
John K.
it would be nice if you always got a second chance
marc seguin
Regular Advisor

Re: Shell error with addition

Hi John,
YES ! I understand now. So i was wrong, and it is a normal behaviour. And the Pharoahs are still the Pharoahs !
Since HP-UX 11 accepts ISOC standards... and not HP-UX 10.20.
(( 07 + 1 )) is calculated as an octal value, but the result is put in a string parameter as a decimal value, so i get 08. And (( 08 + 1 )) is impossible as an octal operation.
CQFD
Steven Sim Kok Leong
Honored Contributor

Re: Shell error with addition

Hi,

If for whatever reasons you do not install the patch, one workaround is to strip the 0 prefix e.g.:

numero=`echo $numero | sed 's/^0//'`

Hope this helps. Regards.

Steven Sim Kok Leong
marc seguin
Regular Advisor

Re: Shell error with addition

Steve,

if you read the description of the patch, it says that a parameter with a leading 0 is interpreted as an octal number IF you INSTALL this patch.
And i don't need the 'sed' tool for a workaround :
numero=${numero##*0}