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