KSH script eval(?) to set variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH script eval(?) to set variable
# 1  
Old 03-20-2009
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
Code:
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:
Code:
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!
# 2  
Old 03-20-2009
Try this:

Code:
eval "ZONENAME=\$zone$ZONENUM"

# 3  
Old 03-20-2009
that didn't work, but pointed me down the right direction. I was able to get it work with

Code:
eval ZONENAME=\$zone$ZONENUM

removing the " " did it! Thanks for your help!!
# 4  
Old 03-20-2009
Strange ...
Actually, if I understand correctly it should be:

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

But I agree that in your case those quotes are not needed.
# 5  
Old 04-28-2009
fabricated output?

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

Code:
eval "ZONENAME=\$zone$ZONENUM"

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

Code:
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):

Code:
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:

Code:
$ 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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Eval set -- and more

Hello everyone, I am taking a course on Lynda and they show this code below. I didn't fully understand some parts. Please see the questions within the code. Thank you for your input and time. Regards, function usage { echo Options are -r -h -b -x --branch --version --help --exclude... (9 Replies)
Discussion started by: mohca2020
9 Replies

2. Shell Programming and Scripting

Getopt eval set parameters not happening when the script is called through an application!

Hi, There is an Informatica tool through which unix scripts can be called. Now the requirement in my project is that a parent script calls a child script but this parent script has to be called through the Informatica tool. In Parent script I'm using TEMP=`getopt -o x:y: -l area:,volume:... (1 Reply)
Discussion started by: Panna
1 Replies

3. Shell Programming and Scripting

ksh - variable to be set to windows path issue

Greetings Experts, I need to pass a parameter to ksh and the value is windows path eg: sh abc.txt C:\Users\chill3chee\Desktop No matter I try with \ delimiter, still could not get this exact value assigned to the shell variable which was checked with echo. Tried with using... (2 Replies)
Discussion started by: chill3chee
2 Replies

4. Shell Programming and Scripting

Sourcing Env file with eval works with ksh but not BASH

Hi, I am running this on Redhat 5.10 I have a simple test script called test.sh which has the following contents and it uses the BASH shebang. ------------------------------------------------------------- #!/bin/bash eval `/tmp/filereader.pl /tmp/envfile.txt` echo "TESTPATH=$TESTPATH" ... (28 Replies)
Discussion started by: waavman
28 Replies

5. Shell Programming and Scripting

set ksh script in restricted mode

Hi, I came across a post wherein you can use "set -r"(on bash) to activate restricted mode ( wherein you cant run some commands such as cd etc). Can anyone guide if we have anything similar in ksh ? Thanks (2 Replies)
Discussion started by: Shivdatta
2 Replies

6. Shell Programming and Scripting

Eval Tricky Manipulation of Arry in KSH - Help

Hi, Could any one share the intelligence to track this problem. I have any array BT_META_36 and it prints properly with contents of array. # print "BT_META_36=${BT_META_36}" # BT_META_36=cab3,cab4:HDS:052,07A cab3,cab4:HDS:052,07A Now I have a BT_META_36 assigned to a variable.... (0 Replies)
Discussion started by: ajilesh
0 Replies

7. Shell Programming and Scripting

Ksh script - Design ? - Search file and set variables

Hi - I'm trying to think of a clever way to write a shell script (trying to stay w/ ksh as that's what I know the best...) that will resolve the following problem: Problem - On a daily basis I have to email folks who are on-call to remind them. I was hoping to script this out so I could have... (9 Replies)
Discussion started by: littlefrog
9 Replies

8. Shell Programming and Scripting

eval a variable that has a .

Hi, Is there any way that I can eval the following - eval abc.csv=def.csv I am getting the - bash: command not found error. thanks. (3 Replies)
Discussion started by: ttshell
3 Replies

9. Shell Programming and Scripting

using eval with set, wc, and expr

Okay this is a mess, I'm trying to assign variables with variables in a for-loop. Here is what i have for code. The syntax is not good. Given the following script: #! /bin/csh foreach site (ABC DEF GHI) eval set \t$${site}sf = ``wc -l \$${site}.sf | awk '{print $1}'`` eval set... (2 Replies)
Discussion started by: wxornot
2 Replies

10. Shell Programming and Scripting

Getting value of variable set in subprocess script

I am writing a shell script that executes another script by fetching it over the network and piping its contents into sh (ftp -o - $script | sh; or wget -O - |sh). Since this bypasses putting the script on the filesystem, this means I can't source the script directly (using . ), but rather it... (1 Reply)
Discussion started by: hadarot
1 Replies
Login or Register to Ask a Question