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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rename / expand variable name 4

Status
Not open for further replies.

mrn

MIS
Apr 27, 2001
3,993
GB
I've got a feeling this is simple, but can't get my brain to work today.

I'm working on AIX 5.2 ksh

How do I expand a variable name

E.G.

a=1
one.$a=`echo hello`

returns

ksh: one.1=hello: not found.



Mike

Unix *is* user friendly. It's just selective about who its friends are.
 
Hello,

if I correctly understand, you are trying to store the result of `echo hello` in a shell variable one.1

There are two problems:

1) A variable name must not contain a .
one_1 is allowed, one.1 isn't.

2) Besides feherke's idea you could try this:
eval one_$a=`echo hello`

regards
 
feherke

one_[$a]=`echo hello` produces output

echo $one_[$a]

[1]

hoinz

Thanks for the . _ pointer
eval not working as I want to be able to call $one_$a

I've posted my script below to show you why I'm trying to do this:

Code:
#! /bin/ksh

clear

lsvg > /saved/vginfo.1

VGINFO=`cat /saved/vginfo.1`

for i in $VGINFO
 do
 lsvg -l $i > /saved/lvinfo.$i
 done

for i in $VGINFO
 do
 vgcount_lp.ksh /saved/lvinfo.$i > tmp/$i.lp.out
 # vgcount_lp.ksh adds column # of LPs
 done

for i in $VGINFO
 do
 vgcount_pv.ksh /saved/lvinfo.$i > tmp/$i.pv.out
 # vgcount_pv.ksh add column # of PVs
 done

for i in $VGINFO
 do
 one_$i=`cat tmp/$i.lp.out`
 done

for i in $VGINFO
 do
 two_$i=`cat tmp/$i.pv.out
 done

for i in $VGINFO
 do
  if [ $one_$i = $two_$i ];then
   echo "Same"
  else
   echo "Diff"
  fi
 done

Just trying to get it to work on two vg's at the moment but will expand to find # of vg's.

Mike

Unix *is* user friendly. It's just selective about who its friends are.
 
You could avoid the whole problem by making your last loop
Code:
for i in $VGINFO
 do
  if [ $(cat tmp/$i.lp.out) = $(cat tmp/$i.pv.out) ];then
   echo "Same"
  else
   echo "Diff"
  fi
 done
but isn't this the same as
Code:
for i in $VGINFO
do
  diff /tmp/$i.lp.out /tmp/$i.pv.out >/dev/null && echo Same || echo "diff"
done

Ceci n'est pas une signature
Columb Healy
 
And what about this ?
a=1
eval one_$a=`echo hello`
eval echo \$one_$a


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Feherke

echo ${one_[$a]} works - Thanks

PHV

eval echo \$one_$a works - Thanks

Columb

I just put the "Same" / "Diff" in to see if it was working but thanks for you input. The above will hopefully be part of a huge health check / server comparision script.

Stars all round I think.....

Mike

Unix *is* user friendly. It's just selective about who its friends are.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top