You are not logged in.
Pages: 1
Hi im having this problem with some varnames..
the thing is: i've set a bunch of variables from a file splitting it into a new var at each newline. it uses a couner at the end
the code i use for setting the var is as following
exec 3< filename
while read <&3
do echo "$REPLY" > /dev/NULL
export VAR$COUNTER="$REPLY"
COUNTER=`echo $COUNTER + 1 | bc`
done
exec 3>&-
The problem i'm having is that when (a bunch of code later) i need these vars the seem not so usefull...
$COUNTER=3
for i in `seq 1 $COUNTER`;
do
echo "saving output"
echo "$VAR$i"
done
i thought this code would result into the following
echo $VAR1
echo $VAR2
echo $VAR3
instead all the code outputs is
1
2
3
I know it's looking for $VAR who doesn't exist
but how can i fix it, what am i doing wrong??
Thanks in advance
Offline
are you sure you're not looking for arrays? http://tldp.org/LDP/abs/html/arrays.html
Offline
You can use eval. For setting the variables:
eval export VAR$COUNTER="$REPLY"
(as a side note, do you need to export the variable?)
And for getting the value:
eval echo \$VAR$i
Or yes, use arrays, as Busata suggests.
Last edited by davvil (2010-04-19 19:13:40)
Offline
set a dynamically named variable : eval varname$i=$value
retrieve a dynamically named variable : ${!varname$i}
Offline
Also you want /dev/null, not /dev/NULL
Offline
Pages: 1