Dynamic variables and eval

Hi,

I need some direction with the following. The below code is semi-psuedo code which will hopefully make it easier to understand what I am trying to achieve:

 
for i in `echo ${testarray
[*]}`
        do
                let c=c+1
                eval "first$c=$i"
                while :
                do
                        let n=n+1
                        echo "Please enter a value for second var as first has already been obtained from array:"                     
                        read second
                        echo "Please enter a value for third var"
                        read third
                        echo "Please enter a value for fourth var"
                        read fourth
                        eval "second$n=${second}
                        echo "Would you like to run the while loop again?"
                        while read input
                        do
                                if [[ $input == "y" || $input == "Y" ]];then
                                        eval "first$c\second$n=${second}${n}"
                                        eval "first$c\second$n\third=${third}"
                                        eval "first$c\second$n\fourth=${fourth}"
                                        break
                                elif [[ $input == "n" || $input == "N" ]];then
                                        break 2
                                else
                                        echo "Invalid response!"
                                fi
                        done
                done
        done

First of all I am looping through an array ( let's call it ${testarray}) with a "for" loop and assigning the value of that element of the array for that iteration to the ubiquitous ${i}. I am also setting up a loopcount variable called ${c} which keeps track of the number of times the "for" loop has been called.

The first stage of the for loop simply assigns the value of ${i} to a more meaningful name (${first}${c}).

I then start a nested while loop with yet another counter called ${n}which, (while true) reads in another variable called "${second}", another called "${third}" and yet another called ${fourth}.

I then use eval to give ${second} a unique name (depending on the iteration. (e.g. for the first iteration, ${second} would be renamed ${second1}, for the second iteration, ${second2}, then ${second3} etc..). ${third} and ${fourth} do not need to be unique for the iteration.

Then, depending on user input I want to EITHER start again at the top of the while loop (thereby keeping the iterative value for ${first}${c}) , but increment the count value of ${second};
OR start the "for" loop again and increment the value of ${first}${c}, followed once again by the other values.

What I hope to end up with is a bunch of dynamic variables of the form (as an example) ${first1second1third} and ${first1second1fourth) or ${first1second2third} etc....

What I am having trouble with is getting the dynamic variables to be named correctly and evaluated by the shell correctly.

Hope this makes sense.....

Thanks in advance

I should use arrays if the numbers of elements of your testarray are not fix, anyway..... this is an example how to use eval:

$ cat eval.sh
#!/bin/sh

string=first
v="123456789"
i=1

eval "$string${i}=$v"
echo $first1
$
$ ./eval.sh
123456789
$

Thanks for your response. I think perhaps I have been too verbose in my explanation. What it boils down to is this.....

If I have the following variables set up for an iteration:
c=1
n=2
sl1=10

Is it possible (with eval or any other tool for that matter) to assign the value of sl1 to an (as yet undefined) variable called $di1sl2.

This would be easy enough if the name of $sl1 and the undefined variable did not change for every iteration.

So the way I see it happening (in psuedo-code) would be:

 
eval "di${c}sl${n} = ${sl${c}}"

Then when you:

echo ${di1sl2}

You should get:

10

Hopefully you can now see my issue, namely that the variable that contains "10" will be differently named for each iteration (it could be $sl1, $sl2 $sl3 etc). so this means that eval will need to deal with a "variable within a variable" - notice the two pairs of curly braces in the same variable in the example above.

Does this make it any clearer?

Many thanks....

$ eval 'di'${c}'sl'${n}='${sl'${c}'}'
$ echo ${di1sl2}
10

Thanks very much both! My eval code now works....

I now see what you mean about using arrays instead of counters....I am going to do that. Very longsighted of you!