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?
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'
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