Passing dynamic variable within another variable.

I have a small program which needs to pass variable dynamically to form the name of a second variable whose value wil be passed on to a third variable.

***************** Program Start ******************

LOC1=/loc1
PAT1IN=/loc2
PAT2IN=/loc3
if [ -f $LOC1/* ]; then
for fpattern in `cat $script/filepattern.txt`
do 
for ffilename in `find $LOC1/ -type f`
do
filename=`echo $ffilename | cut -d "/" -f 3`
file=`echo $filename | cut -d "_" -f 1`
case "$file" in
$fpattern)
INLOC=${${file}IN}
echo $INLOC
echo $filename
;;
esac
done
done 
else
echo "Exiting Job as no files present."
exit
fi

***************** Program End ******************

List of files present in the /loc1 are as below

/loc1/PAT1_test.txt
/loc1/PAT2_test1.txt

content of filepattern.txt are as below

PAT1
PAT2

when i execute the program, i keep getting this error

INLOC=${${file}IN}: bad substitution

Please let me know if i am doing something wrong. or is there an alternate way to achieve this without having to use to much variables.

expected output is:

/loc2
PAT1_test.txt
/loc3
PAT2_test1.txt

What is this INLOC=${${file}IN} ???

I am not seeing any point of this line :frowning:

INLOC=${${file}IN}

will become a common variable within a seperate function repeteadly based on the different values of ${file}. The main issue i am facing here is that i am not able to set a value for INLOC.

Shells do not usually support this programming style, and you should be very clear in the first place about what you want to achieve. You could consider using associative arrays available in recent shells, or using bash's indirect expansion (c.f. man bash).
As a very last resort, you could try to eval that expression, but you should be aware that using eval can be dangerous!

1 Like

Try:

INLOC="${file}IN"
1 Like

Thank you all for your valuable help.

RudiC, Took your advice modified the line to

eval INLOC='$'{${file}IN}

and it worked beautifully.

I have got the desired output from the program now.:):b: