Problem with dynamic variables outside the loop

Hi All,

Shell is ksh

I've given portion of the script here to explain the problem.

It will accept 2 input parameters .

in_file1=$1 
in_file2=$2 
outbound_dir=/home/outbound 

for i in 1 2 
do 
  eval file$i=$outbound_dir/\$in_file$i 
  eval echo "filename is \$file$i" 
  eval temp_file=$outbound_dir/\$in_file$i 
  eval FILE$i_LINE_COUNT=`wc -l < $temp_file` 
  eval echo "Total lines in file$i are \$FILE$i_LINE_COUNT" 
done 

echo "file1 name outside loop is $file1" 
echo "file1 count outside loop is $FILE1_LINE_COUNT" 

When i am displaying file1 and $FILE1_LINE_COUNT variables inside the loop its giving correct values.

But ouside the loop getting correct value for only file1 variable not getting for $FILE1_LINE_COUNT variable.

can you pls help me.

First off, please format your post so it is readable. Not reading your post very closely, but based on the last sentence, have a look at http://www.unix.com/shell-programming-scripting/116410-counting-items-variables-how-come-works-however.html

Hi Peterro,Formatted now. Can you pls have a look at it now.

Better, please use the code tags for code.

In any case, this has nothing to do with the loop specifically. Replace

eval FILE$i_LINE_COUNT=`wc -l < $temp_file`

with

eval FILE${i}_LINE_COUNT=`wc -l < $temp_file`

You were assigning the word count output to $FILE since $i_LINE_COUNT was undefined. Surround the 'i' with curly braces to distinguish it from the rest of the line.

thanks for your reply....it works fine with curly braces.

eval file$i=$outbound_dir/\$in_file$i
eval echo "filename is \$file$i"
eval temp_file=$outbound_dir/\$in_file$i

eval FILE${i}_LINE_COUNT=`wc -l < $temp_file`

Can i use $file$i instead of $temp_file in the above line ??