evaluating a variable inside a variable

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

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

 
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

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

and

 
 
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

This:

# eval "myVar=\$${i}"
# echo "myVar: [${myVar}]"

"myVar" will have the content of variable pointed by: "${i}". But be careful: eval is evil!

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:

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

thanks so much (it was driving me a little crazy)