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!

Creating variables from the values of other variables 1

Status
Not open for further replies.

derekpc

Technical User
Jul 3, 2001
44
GB
I am trying to assign variables using the value
of other variables and not getting what I want
can someone point me in the right direction
please?
This is what I have tried:
I have a list of parameters in a file called /home/userid/paramdir/params to process in a new line delimited file as follows:
Code:
param1
param2
param3
param4
param5
script1
Code:
#!/bin/ksh
basepath=/home/userid/output
paramdir=/home/userid/paramdir
for test1 in first second
do
   if [[ $test1 = "first ]]
   then
      for test2 in alpha bravo charlie delta
      do                           ${test1}_${$test2}_hold=${basepath}/${test2}/${test2}/out/
# the preceding evaluates as I expect it to
         for param in $(cat ${paramdir}/params)
         do
            script2 $param $test1 $test2
         done
      done
   fi
   if [[ $test1 = "second" ]]
   then
      for test2 in echo foxtrot golf hotel
      do
         for param in $(cat ${paramdir}/params)
         do
            script2 $param $test1 $test2
         done
      done
   fi
done

script2

Code:
#!/bin/ksh
param="$1"
test1="$2"
test2="$3"

${param}_output=/home/userid/${test1}/${test2}/${param}_target
if [[ ! -d ${param}_output ]]
then
   mkdir -p ${param}_output 
   #this ends up creating ./param1_output   # through ./param5_output
   chown userid:groupid ${param}_output
fi
print "Some output" > ${param}_output/index.html

What I want to happen is for index.html to be printed to /home/userid/first/alpha/param1_target/index.html for the first instance through /home/userid/second/hotel/param5_target/index.html for the last instance. But what I am getting is an attempt to print index.html to ./param1_output through ./param5_output.

I tried using eval as
Code:
 eval ${param}_output=/home/userid/${test1}/${test2}/${param}_target
but ended up with the same results. I am afraid that at this point I am stumped.
Any suggestions will be greatfully received.
Thanks.
Derek.
 
Try this:
script2
#!/bin/ksh
param="$1"
test1="$2"
test2="$3"
output=/home/userid/${test1}/${test2}/${param}_target
if [[ ! -d $output ]]
then
mkdir -p $output
chown userid:groupid $output
fi
print "Some output" > $output/index.html
Otherwise in man ksh pay attention to the eval builtin.

Hope This Help
PH.
 
Thanks PH,
The probelm is that I would like $param prepended to _output and have that variable (${param}_output) evaluate to the full path. The actual script has needs the the $param to find some other information. I've read the man ksh man page several times. Guess I am particulalry dense today. :-(
Derek
 
Try this in script2:
set -x
eval ${param}_output='/home/userid/${test1}/${test2}/${param}_target'
set +x

Hope This Help
PH.
 
When ever I have this type of problem to solve I set a string first then eval the string. This will allow you to echo the value of the string before the eval is done.

[tt]CMDSTRG="${test1}_${test2}_hold=${basepath}/${test2}/${test2}/out/"
eval ${CMDSTRG} [/tt]


I made some changes to your scripts so I could run it in a local directory but it should do what you need it to do
SCRIPT1
[tt] #!/bin/ksh
basepath=home/userid/output
paramdir=home/userid/paramdir
for test1 in first second
do
if [[ $test1 = "first" ]]
then
for test2 in alpha bravo charlie delta
do
CMDSTRG="${test1}_${test2}_hold=${basepath}/${test2}/${test2}/out/"
eval ${CMDSTRG}
# the preceding evaluates as I expect it to
for param in $(cat ${paramdir}/params)
do
script2 $param $test1 $test2
done
done
fi
if [[ $test1 = "second" ]]
then
for test2 in echo foxtrot golf hotel
do
for param in $(cat ${paramdir}/params)
do
script2 $param $test1 $test2
done
done
fi
done [/tt]

SCRIPT2
[tt]

#!/bin/ksh
param="$1"
test1="$2"
test2="$3"

CMDSTRG=${param}_output=home/userid/${test1}/${test2}/${param}_target
eval ${CMDSTRG}
DIRCHECK=$(eval echo \$${param}_output)
if [[ ! -d ${DIRCHECK} ]]
then
mkdir -p ${DIRCHECK}
#this ends up creating ./param1_output
# through ./param5_output
chown userid:groupid ${DIRCHECK}
fi
print "Some output" > ${param}_output/index.html [/tt]


I did it kinda quick so HOPE IT WORKS !

JRjr [morning]
 
PH and JR,
Thank you both very much for your assistance. Looking at PR's second post made me go back and take another look at my attempted use of eval and my debugging output. Turned out that I was assigning the value I needed when using eval, just wasn't retreiving the value properly. I ended up getting quite close to JR's suggestion (didn't think of the CMDSTRING idea but that looks a good idea) during the day today. I ended up with the following which worked a treat:
In script 1
Code:
eval ${test1}_${$test2}_hold=${basepath}/${test2}/${test2}/out/
and to pull the value
Code:
eval \\$${test1}_${$test2}_hold

in script 2
Code:
eval ${param}_output=/home/userid/${test1}/${test2}/${param}_target
eval outputdir=\$${param}_output
if [[ ! -d  $outputdir ]]
then
   mkdir -p $outputdir
   chown userid:groupid $outputdir
fi
print "Sometext output" > $outputdir

Basically it just took rereading everything I had on eval about 16 times and your posts for it to finally dawn on me to use \$$ to my value back. [blush]
Thanks again,
Derek
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top