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!

Dynamic Variable Evaluation 1

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
Hello,

On a Sun Solaris 8 system in a Bourne script I am dynamically creating and testing some variables. The problem I am having is detecting when/if a dynamic variable value does not have a value.

The assumption is that there will always be a DEST1 but the goal is to test if there are more. It is also assumed that they will be created in sequential order (DEST1, DEST2 etc.).
Code:
[COLOR=green]#User Defined Variables[/color]
DEST1="/dir_x"
DEST2="/dir_y"

[COLOR=green]# Build Destination String[/color]
for i in 1 2 3 4
do
   echo "$DEST$i" [COLOR=green]# <=== This displays correctly[/color]
   [b]if [ -n $DEST$i ] then[/b]
      [COLOR=green]# Stuff I need to do if above true[/color]
       .
       .
       .
   fi
done

In this snippet, how can I test if the user created a DESTn variable, the above if does not seem to work?

Thanks,

Michael42
 
if [ -n "$DEST$i" ]; then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Try this...
Code:
#!/bin/sh

#User Defined Variables
DEST1="/dir_x"
DEST2="/dir_y"

# Build Destination String
for i in 1 2
do
   eval DIR=\$DEST$i
   echo "$DIR"
   if [ -n $DIR ] ; then
      # Stuff I need to do if above true
      echo "Dir = ${DIR}"
   fi
done
Hope this helps.
 
SamBones,

Bingo! eval and the syntax you used for it did the trick. :)

Thanks for taking the time to post,

Michael42
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top