evaluating a variable inside a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting evaluating a variable inside a variable
# 1  
Old 12-01-2011
evaluating a variable inside a variable

Hi there, i think im getting myself a little confused and need some help Smilie



I am reading in a bunch of variables to my script from an external file and need to validate that a value has been set for each

so if you can imagine, the user is required to pass in 4 values username,password,uid and gid. these get set to


$USER
$PASS
$UID
$GID

now this is fine, however I want to write a little loop that tests for its existence and echos an error if its not set. I appreciate I can do this as individual lines, but in reality there are more than 4 variables, there are hundreds, so id like to do it in a loop

so the idea being something like

Code:
 
for i in "USER PASS UID GID"; do
      if [ -z "$i" ] ; then
           echo $you have not entered a value for $i"
           exit 1
      fi
done

now i realise that this wont work because when $i is expanded it is just a string and not another varuable, but ive tried
Code:
 
 
if [ -z "${$i}" ] ; then

and
Code:
 
 
if [ -z "$${i}" ] ; then

etc

would I need to use 'eval' and if so, can someone give me an example of how i would do this?

any help would be greatly appreciated
# 2  
Old 12-01-2011
This:
Code:
# eval "myVar=\$${i}"
# echo "myVar: [${myVar}]"

"myVar" will have the content of variable pointed by: "${i}". But be careful: eval is evil!
# 3  
Old 12-01-2011
Using eval on data you don't control is an extremely bad idea. Ask bobby tables.

If you have BASH or KSH, you can do this:

Code:
for VAR in USER PASS UID GID
do
        if [ -z "${!VAR}" ]
        then
                echo "$VAR not set"
                exit 1
        fi
done

# 4  
Old 12-01-2011
thanks so much (it was driving me a little crazy)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 Replies

2. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

3. Shell Programming and Scripting

Evaluating a variable

Does anyone know of a way to force a variable name held in another variable to return the value of the first variable? Best if I give an example, that does not work: /usr/local/bin >cat mike.sh NUM1ref=16 NUM2ref=32 echo "==============" for VAR in NUM1 NUM2 do XXXX=${VAR}ref echo $XXXX... (4 Replies)
Discussion started by: mikejordan
4 Replies

4. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

5. Shell Programming and Scripting

passing a variable inside another variable.

Any help would be great. I know this is a dumb way of doing this, but I would like to know if there is a solution doing it this way. I'm very new at this and I'd like to learn more. Thanks! :D:D count=0 while ; do echo "enter your name" read name_$count let count=count+1 done ... (2 Replies)
Discussion started by: reconflux
2 Replies

6. Shell Programming and Scripting

K Shell evaluating value to a variable

Hi, I have the following requirement. V="First" R="V" echo $$R The output should be First. How do i achieve this. how do we evaluate the $R and evaluate it to $V as $R contains V and $V is First. Thanks Vijay (2 Replies)
Discussion started by: vijaykrc
2 Replies

7. Shell Programming and Scripting

Bash: evaluating $? variable (if statement)

Hello, i'm unable to write a correct if... statement to evaluate the $? variable. Could anybody send to me an example? for example, this lines of code didn't work... if ; then etc etc if ; then etc etc Thank you in advanced. (5 Replies)
Discussion started by: aristegui
5 Replies

8. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies

9. Shell Programming and Scripting

How to replace variable inside the variable

hi sir, i need your help for this script inside /rnmucdr/ednms05/ken/xMNBDF045_Script.sql content variable like this select * from invoice where bill_date=$BILLDATE and startNum=$STARTPARTNNUM and total_partn=$TOTALPARTN if i just paste this replace with the $SCRIPT it works great,if... (31 Replies)
Discussion started by: mani_um
31 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question