Variable values within for loop

Hi All

I am trying to fetch the size of three files into three separate variables within a for loop and am doing something like this:

for i in ATT1 ATT2 ATT3
do
size_$i=`ls -ltr $i | awk '{print $5}'`
echo ${size_$i}
done

but am getting the below error:

ksh[3]: size_ATT1=522:  not found
ksh[3]: size_ATT2=2170:  not found
ksh[3]: size_ATT3=874:  not found

Any idea how to fix the "not found" error?

Thanks in advance

Try:

for i in 1 2 3
do
  size=$(ls -l ATT$i | awk '{print $5}')
  echo "${size}"
done

or, since you are using KornShell:

for i in 1 2 3
do
  ls -l ATT$i | read x x x x size x
  echo "${size}"
done

I tried both the options but am getting

ksh[3]: ATT1: bad number

The name of each file should get suffixed to the variable being used i.e. size_ATT1 and the echo should return the value stored in the variable as in the size in this case. So the output should be something like:

size_ATT1=522
size_ATT2=2170
size_ATT3=874

and the echo should only return the values:

522
2170
874

Not sure if it can be made to work this way

Try using either BASH as shebang or rewrite the shown code to match ksh syntax.
Or, use the array-suggestion from scrutinizer.

Assigning values to a variablename formed by using variables is a complex thing, well, not really, but risky and prone to errors, and should be avoided where possible.
You could write the generated string (all 3 lines of size_ATT{1..3}=...) by writing it to a file, then source it.

hth