Re: Way OT: getting values of list of variables
Thus spake Jeremy on Mon, Sep 24, 2001 at 01:27:16AM -0500:
>
> Hi,
Good Morning,
> <...>
> I have a series of variables that I'm importing to a bash script from another
> file, and I need a way to run a loop to echo the contents of each one in
> sequence. As an example, say this file has the following:
>
> var1=data1
> var2=data2
> var3=data3
> var4=data4
>
> Is there some way I can make my script start with var1 and echo each one
> through var4 WITHOUT hardcoding each seperate variable into the script?
> <...>
I'm not sure if I understand you right, but I think the 'eval' builtin
could possibly help you:
#!/bin/sh
read a
while [ -n "$a" ];
do
eval "$a" # assigns 'data1' to 'var1', etc...
# getting the names of the variables:
tmp_var=$( echo "$a" | cut -d\= -f 1 )
# and the datas:
tmp_data=$( echo "$a" | cut -d\= -f 2 )
eval "$tmp_var=foobar" # does var1=foobar
read a
done
echo $var1 # should output "foobar"
> Thanks,
> Jeremy
I don't know what you intend to do with this, so I can't help you more,
HTH,
Romain
--
"To YOU I'm an atheist; to God, I'm the Loyal Opposition."
-- Woody Allen
Reply to: