bash loops

hello

i'm writing a script and I want to use a for loop inside a while loop as following:

while read line; 
do	
    echo $line
	
    for i in $vrm;
	do
	    echo $i
        done
done < './contacts'

when i use just the while loop it prints the lines from file ./contacts just fine
but when i put the for loop,first it prints the values of the variable $vrm and then the $line.Even more if i put an if statement inside the for loop it only works for the first line of the file.
Is there a priority of the loops?
Or am I doing something wrong in the script?

Maybe the first line in contacts is an empty line?

No,no it's not..
My syntax though seems ok right?

Yes, it looks OK, apart from not double quoting the variable references. The semicolons are only required when do is on the same line, but they won't hurt.

actually I tried to figure out whats going on and when I put a simple echo command inside the if statement it is ok
when,instead of the echo,I try to assign a variable like this:

mlt="$(./getmt $1 $2 $i)"

after it goes only one time inside the if statement and calls the function then it stops the WHILE loop although it finishes the for loop.

The assignment is ok,I checked it alone and inside the for loop without the while loop and works fine.
So once it goes in the IF it finishes the FOR but not the WHILE loop
and that happens only with the assignment not with the echo command...

while read line; 
do	
    echo $line
	
    for i in $vrm;
	do
	     echo $i
             if then
                 mlt="$(./getmt $1 $2 $i)"                 
             fi
        done
done < './contacts'

The if statement is not valid syntax

this is how I do it



arr=($line)
for i in $vrm;
	do
	     echo $i

             if [[ "${arr[N-1]}" = "$i" ]]; then
                 mlt="$(./getmt $1 $2 $i)"  
             fi
       done

and the command that causes the problem is in ./getmt
and is:

lt="$(rsh $2 w $3 )"

If i put an echo instead of "lt" assignment in ./getmt runs for every line of while loop.Also if I put an echo instead of calling the ./getmt it also runs for every line of while loop.
Only when I do the assignment of lt it stops once it goes inside the if statement..And again,it stops the while loop,it finishes the for loop..
It really drives me crazy!Why does it stop?Please help

Try using

set -x

at the beginning of either script to see what is happening..

I removed the rsh and worked ok
Why is that?
I need the rsh in order to access remote terminals..

You can also put a set -x; right before the rsh statement to find out.

It keeps returning a mistake

1260+: syntax error: operand expected (error token is "+")

for a command :


total=$(($ttotal+$mns))

even when I replace the "+" with a white space

Is that the output you're getting with lt="$(set -x; rsh $2 w $3 )" ?

no,
I changed the loops and I put the while loop inside the for loop

This thread has strayed off too far from the original subject and does not appear to go anywhere. I am closing it.