KSH script eval(?) to set variable

first of all, thanks to all on this board, it has been a huge resource to answer most of my questions!

I am stuck on something that should really be simple, and was looking for some help.. I am using KSH on solaris and working on a script to move containers from server to server. Where i am having problems is setting a variable that should equal a different variable based on the answer..

ie

print -n "Please select the the zone to move     : "; read ZONENUM
print $zone6 <-this was here for testing to show me the variable is right
ZONENAME=`eval '$'zone$ZONENUM`
print $ZONENAME

basically i have $zone1 $zone2 etc set in the script already, so i just need $ZONENAME=$zone(?) based on which number they select

when i run it i get this:

Number            Zone Name
===================================================
(1)              sunstage1
(2)              zndvap03
(3)              znbrocade1
(4)              sundev1
(5)              znqaap02
(6)              zndvux01
(7)              zndvux02

Please select the the zone to move     : 7
zndvux01
./zonemgmt2[28]: zndvux02:  not found

so it looks like it is close... Where am i going wrong, or is there a better way to accomplish this?

Thanks!

Try this:

eval "ZONENAME=\$zone$ZONENUM"

that didn't work, but pointed me down the right direction. I was able to get it work with

eval ZONENAME=\$zone$ZONENUM

removing the " " did it! Thanks for your help!!

Strange ...
Actually, if I understand correctly it should be:

$ ZONENUM=zndvux02 zonezndvux02=OK
$ eval "ZONENAME=\$zone$ZONENUM" 
$ print $ZONENAME
OK

But I agree that in your case those quotes are not needed.

In the following line, the quotes are no-ops.

eval "ZONENAME=\$zone$ZONENUM"

They get stripped away, resulting in the following line, which eval then interprets:

ZONENAME=$zonezndvux02

Even if there was whitespace in ZONENUM, the quotations wouldn't protect it from eval. Radoulov probably had in mind something like the following (the absence of an escaped dollar sign changes everything):

var1="value with whitespace"
eval ZONENAME=\"$var1\"

But of course, in this toy example, eval would not be needed.

Notice that in the case of variable assignment, word splitting is not performed:

$ var1="value with spaces"
$ var2=$var1
$ echo $var2
value with spaces

more examples at my Bash page (most apply to ksh as well):
https://www.pooryorick.com/secure/wiki/Pub/Bash