Building Variable Names in Bourne Shell

Alen Shapiro alen at cogen.UUCP
Thu Mar 24 08:47:41 AEST 1988


In article <12565 at brl-adm.ARPA> kevinc at bearcat.lim.tek.com (Kevin Cosgrove 627-5212) writes:
>Anyone know how to build *accessible* variable names on the fly in Bourne
>shell?  Below is a do nothing script which, I hope, demonstrates what I
>want to do.
>
>#!/bin/sh
>onestring="A"			# set the values of arbitrary strings
>twostring="B"
>threestring="C"
>for string in one two three	# loop thru string sets
>do
>   var="${string}string"	# var contains the name of the string
>				# I wish to get access to
>
>   echo "\$$var = ${var}"	# how do I get the contents of the string
>				# "pointed to" by var?
>   echo
>done
>
>Yes, I know there are lots of other shells and/or languages which make
>the above easy to do, but I'm trying to patch existing scripts, and this
>would make it much easier to do.


Nice exercise - try this. BTW /bin/calendar is a GREAT crib sheet for 
such things 
 
#!/bin/sh 
onestring="A"
twostring="B"
threestring="C"
for string in one two three
do
    var="a=\$${string}string"
    eval $var
    echo ${string}string = $a
done
 
--alen the Lisa slayer (it's a long story)

In
te
re
st
in
g
li
ne
fi
ll
er
go
es
he
re



More information about the Comp.unix.wizards mailing list