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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array and variable in Korn Shell 2

Status
Not open for further replies.
Sep 1, 2003
10
NL
Hi,

I'm trying to create a script with a variable array. First I'll set the array's. After that I'm going into a loop so I can call all three array's in one pass.

set -A system1 250 1.2.3.4 routerUS01.nl.com
set -A system2 250 2.3.4.5 router2501.nl.com

echo ${system1[0]}"; "${system1[1]}"; "${system1[2]}"."
typeset -i arraywaarde=1
while [ ${arraywaarde} -lt 3 ]
do
vrarray0=\$\{system${arraywaarde}"[0]"\}; echo ${vrarray0}
vrarray1=\$\{system${arraywaarde}"[1]"\}; echo ${vrarray1}
vrarray2=\$\{system${arraywaarde}"[2]"\}; echo ${vrarray2}
arraywaarde=${arraywaarde}+1
done

When I execute the script, I get the following results:
250; 1.2.3.4; routerUS01.nl.com.
${system1[0]}
${system1[1]}
${system1[2]}
${system2[0]}
${system2[1]}
${system2[2]}

I expected to get:

250; 1.2.3.4; routerUS01.nl.com.
250
1.2.3.4
routerUS01.nl.com
250
2.3.4.5
router2501.nl.com

Why am I getting the name of the variable instead of the value? How can this be solved. I tried lots but nothing seems to work anymore.

 
You've escaped the $ and the { , rendering them as the actual $ sign and { sign
Why ?

Dickie Bird (:)-)))
 
Try using eval...

vrarray0=\$\{system${arraywaarde}"[0]"\}; eval echo ${vrarray0}
vrarray1=\$\{system${arraywaarde}"[1]"\}; eval echo ${vrarray1}
vrarray2=\$\{system${arraywaarde}"[2]"\}; eval echo ${vrarray2}
 
To dickiebird:
If I remove the escape characters then the following error occurred:
250; 1.2.3.4; routerUS01.nl.com.
./lol[8]: vrarray0=${system${arraywaarde}"[0]"}: The specified substitution is not valid for this command.

To Ygor:
eval did the solution. Works now:

250; 1.2.3.4; routerUS01.nl.com.
250
1.2.3.4
routerUS01.nl.com
250
2.3.4.5
router2501.nl.com

Thx both for the assistence.
 
vrarray1=\$\{system${arraywaarde}"[1]"\}; has to be evaluated by the shell twice, the first time to replace "system${arraywaarde}" with system0, for example, and the second time to get the contents of the array.

In my solution I put the eval in a different place, but Ygor's works equally well.

Also arraywaarde=${arraywaarde}+1 would only set the value of arraywaarde to "0+1" instead of "1". You need to surround it in $(( ... )) to actually calculate the new value, or alternatively use ((arraywaarde=arraywaarde+1)).


[tt]#!/bin/ksh
set -A system1 250 1.2.3.4 routerUS01.nl.com
set -A system2 250 2.3.4.5 router2501.nl.com

echo ${system1[0]}"; "${system1[1]}"; "${system1[2]}"."
typeset -i arraywaarde=1
while [ ${arraywaarde} -lt 3 ]
do
eval vrarray0=\$\{system${arraywaarde}"[0]"\}; echo ${vrarray0}
eval vrarray1=\$\{system${arraywaarde}"[1]"\}; echo ${vrarray1}
eval vrarray2=\$\{system${arraywaarde}"[2]"\}; echo ${vrarray2}
arraywaarde=$((${arraywaarde}+1))
done[/tt]

Annihilannic.
 
Thx Annihilannic for the solution. Works also, and seems a better (read neater) solution. Off course the vars vrarray* will be used through the entire script, so your solution will be best, since the eval should run only once.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top