TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   Problem with while loop that never stops (http://forums.techarena.in/software-development/1263263.htm)

Mindstorm 26-10-2009 04:49 PM

Problem with while loop that never stops
 
I have a shell file with me in which I have a "while" loop that never stops:

I then have a file ping.txt with 4 ip in 4 lines and when I do a echo $NBS_LINE I have 4 lines

Code:

#!/bin/bash
FILE="/home/test/ping.txt"
NBS_LINE=`wc -l ping.txt | cut -d" " -f1`
i=1
while [ $i=$NBS_LINE ]
do
        HOSTNAME=`head -n $i ping.txt | tail -n 1`
        /bin/ping -c3 $HOSTNAME
        if [ $? != 1 ]
        then
                echo "$HOSTNAME;YES"
        else
                echo "$HOSTNAME;NO"
        fi
i=$(($i + 1))
done

but the problem is that when it reaches the last line does not stop. Have you an idea of the problem?

Reegan 26-10-2009 04:51 PM

Re: Problem with while loop that never stops
 
Why not you use $ i <$ NBR_LINE as a condition? Because there I even understand why your program works since $ i! = $ NBS_LINE the first execution

Mindstorm 26-10-2009 04:51 PM

Re: Problem with while loop that never stops
 
If I shall be as you say it makes me:

./script_ping.sh: line 11: 4: No such file or directory type

I do not know all of the report.

Reegan 26-10-2009 04:53 PM

Re: Problem with while loop that never stops
 
Sorry, you're absolutely right, I spoke too soon

So here is the structure that I think you will need to use

Code:

$i = 1;
 
while [ $i -lt $NBS_LINE ];
 do
  Processing ...
  let i+=1
done

The error message it gives you is logical because the operator "<" is a redirection of flows, not the operator "less than" (which is-lt).


All times are GMT +5.5. The time now is 07:48 PM.