1752754 Members
4906 Online
108789 Solutions
New Discussion юеВ

shell error?

 
SOLVED
Go to solution

shell error?

I've tried this script:
root>cat test_num.sh
for numero_sel in 01 02 03 04 05 06 07 08 09 10 11 12
do
((numero_sel=$numero_sel+1))
echo $numero_sel
done
the original script take value from the current month(mese=`date +%m`) and add 0 to make 01,02,... in numeric.
the script gave me an error when months = 08 or 09 with other months it works.

Is this an error of the shell???

I' tried it on HP-UX 11.00 and 11.11 with the same result.

Have someone any idea??

Thanks Antonio

root>sh -x test_num.sh
+ (( numero_sel=01+1 ))
+ echo 2
2
+ (( numero_sel=02+1 ))
+ echo 3
3
+ (( numero_sel=03+1 ))
+ echo 4
4
+ (( numero_sel=04+1 ))
+ echo 5
5
+ (( numero_sel=05+1 ))
+ echo 6
6
+ (( numero_sel=06+1 ))
+ echo 7
7
+ (( numero_sel=07+1 ))
+ echo 8
8
+ (( numero_sel=08+1 ))
test_num.sh[3]: 08+1: The specified number is not valid for this command.
5 REPLIES 5
Chris Wilshaw
Honored Contributor

Re: shell error?

You could try using expr to get round this

eg:

numero_sel=`expr $numero_sel + 1`

Re: shell error?

I've solved the problem with an

if [ $numero_sel = 08 ] ; then
numero_sel=9
else
if [ $numero_sel = 09 ] ; then
numero_sel=10
else
((numero_sel=$numero_sel+1))
fi
fi

my question is why the error with 08 an 09?
there is a patch?
my script is invalid?
James R. Ferguson
Acclaimed Contributor
Solution

Re: shell error?

Hi Antonio:

This is a recent change in the shell's behavior to conform with ISOC standards, namely that numbers starting with '0' should be recognized as octal and numbers starting with '0x' or 'OX' should be should be recognized as hexadecimal.

Drop the leading '0' from your numbers and all will work as intended.

See, for instance, PHCO_28831 for 11.0 or PHCO_27345 for 11.11 for more information.

Regards!

...JRF...
Leif Halvarsson_2
Honored Contributor

Re: shell error?

Hi,
Strange, I tested on HP-UX 11 and I can't reproduce your problem. Maybe a patch problem.
Bill Hassell
Honored Contributor

Re: shell error?

This is definitely a recent patch change and even caused some HP startup scripts to fail. Unix shells have always defined hex numbers as values beginning with 0x (ie, 0x10 is 16 in decimal) and octal numbers as values that start with a leading zero (ie, 08 is invalid but 010 is OK and equals 8 decimal). What this patch does is to bring the shells into conformance with the representation standards. This will definitely cause some old scripts to fail. A good script writer will make use of typeset as a consistent method for representing data.


Bill Hassell, sysadmin