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!

Array problem 1

Status
Not open for further replies.

LGJ

Programmer
Mar 7, 2003
50
GB
Hi all,

Please could I get some help with a problem I am having with a shell script, the code is below:

====================================================

#!/usr/bin/ksh

set -A array0 green white yellow
set -A array1 dog cat rabbit
set -A array2 zero one two

lj="${array0[1]}"
echo ${lj}

X=3
COUNT=0
while [[ ${COUNT} < ${X} ]]
do
temp="\${array${COUNT}[1]}"
echo ${temp}
(( COUNT=COUNT + 1 ))
done

==================================
This gives the following output:

white
${array0[1]}
${array1[1]}
${array2[1]}


Whereas what I actually want is:
white
white
cat
one

==========================================
I want something that can go through the arrayN and get the value at index[1], if I had say 100 arrays then using the temp and count variables would help but all I seem to get back is the string?

Please point me in the correct direction and help with any pointers if my scripting style is poor. I haven't wrote many scripts and just learn as I go.

Thanks

LGJ
 
Replace this:
temp="\${array${COUNT}[1]}"
By this:
eval temp="\${array${COUNT}[1]}"

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

I will remember the use of eval from now on.

LGJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top