variable list of variables

Jim Webb jrw at mtune.att.com
Sat Mar 9 08:15:36 AEST 1991


In article <robi.668225101 at thomas> robi at 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 at mtune.att.com

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



More information about the Comp.unix.shell mailing list