[comp.unix.shell] variable list of variables

robi@bacchus.esa.oz.au (RoBeRt KaRp) (03/06/91)

All below is in Bourne shell.
I am trying to acheive the following:

users="robi henry matt"
for person in $users
do
	${person}_files="something that gives me a list of files say x.c y.c z.c"
done

What I want to do now is get to the list of files. Something like:

for file in ${${person}_files}
do
	something_to_do_with_file
done

However, I can't access this, ${${person}_files}, variable. The
shell tells me bad substitution for the above and I can't think
of a way to get ${robi_files} (say) out of this.

Any help would be appreciated,

Thanx,

	- Robi
--
INTERNET: robi@bacchus.esa.oz.au    ACSnet: robi@bacchus.esa.oz
Tel     : (+61) (3) 819 4554             Robert Karp///
Fax     : (+61) (3) 819 5580                      ////
UUCP    : uunet!bacchus.esa.oz.au!robi          \XXX/

jrw@mtune.att.com (Jim Webb) (03/09/91)

In article <robi.668225101@thomas> robi@bacchus.esa.oz.au (RoBeRt KaRp) writes:
>All below is in Bourne shell.
>I am trying to acheive the following:
>
>users="robi henry matt"
>for person in $users
>do
>	${person}_files="something that gives me a list of files say x.c y.c z.c"
>done
>
>What I want to do now is get to the list of files. Something like:
>
>for file in ${${person}_files}
>do
>	something_to_do_with_file
>done
>
>However, I can't access this, ${${person}_files}, variable. The
>shell tells me bad substitution for the above and I can't think
>of a way to get ${robi_files} (say) out of this.

What you need to do is fiddle with the sh builtin "eval" to do this.

One quickly hacked way is the following:

    users="robi henry matt"
    
    for person in $users
    do
        # you need both sets of quotes (' & ") otherwise eval will
        # think you are setting person_files to one thing, and then
        # a command composed of the rest of the files, eg it will
	# think you mean robi_files=x.c y.c z.c instead of
        # robifiles="x.c y.c z.c"
        eval ${person}_files='"${person}x.c ${person}y.c ${person}z.c"'
    done
    
    for person in $users
    do
        echo "${person}_files: \c"
        for i in `eval echo \\$\${person}_files`
        do
        	echo "$i \c"
        done
        echo
    done

Now, I am sure I've added one too many steps in this mess (at least) but it's
getting into the weekend, so, you can fiddle with it :-) 

Later,

-- 
comp.windows.open-look -&- alt.sex.pictures.d                         Jim Webb
alt.sex.bondage -&- comp.graphics.visualization              jrw@mtune.att.com

Sex and voyeurism and fantasy...  Those who can't do, watch and imagine. --ccs

jik@athena.mit.edu (Jonathan I. Kamens) (03/11/91)

Use "eval" to get what you want.  For example:

$ jik_files="a.c b.c c.c"
$ echo $USER
jik
$ for file in `eval echo '$'${USER}_files`; do echo $file; done
a.c
b.c
c.c
$

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710