Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use Array and Arithmetic for in KSH?

Status
Not open for further replies.

micotao

Programmer
Jun 16, 2004
33
CN
Hi,all:
Please look at the following ksh script.
Code:
#!/bin/ksh
set -A name 1 2 3 4 5
NUM_MAX=5
for ((i = 0; i <= NUM_MAX-1; i++))
do
    echo ${A[$i]}
done
I can't see obvious errors, but in fact, I got the following syntax error reporting:
./test[4]: syntax error at line 4 : `((' unexpected

Can you please help to look into this issue?
How to use arithmetic for? Is there anything wrong with my usage?

Thanks a lot.

 
Something like this ?
set -A name 1 2 3 4 5
NUM_MAX=${#name[*]}; typeset -i i=0
while [[ $i < $NUM_MAX ]]
do
echo ${name[$i]}
(( i+=1 ))
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
micotao,

The arithmetic form of for is only available in ksh93 compliant versions of ksh. ESC CTL-V at the command prompt should give you the version date. If it's equal to or earlier than 11/16/88, the form you used won't work.

Another test would be to attempt the assignment:

foo.bar="test"

If you get an error message, it's not ksh93.

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

 
PHV
QUOTE "while [[ $i < $NUM_MAX ]]"

should it be not ?
while (( $i < $NUM_MAX ))

can be tricky sometimes.. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top