Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 26-10-2009
Member
 
Join Date: May 2009
Posts: 640
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?
Reply With Quote
  #2  
Old 26-10-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
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
Reply With Quote
  #3  
Old 26-10-2009
Member
 
Join Date: May 2009
Posts: 640
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.
Reply With Quote
  #4  
Old 26-10-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
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).
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem with while loop that never stops"
Thread Thread Starter Forum Replies Last Post
Watercooling: Single loop or Dual Loop Akolekar Hardware Peripherals 3 21-10-2011 10:52 PM
Game loop problem with XNA Botan Software Development 5 30-11-2010 05:35 AM
Problem of infinite loop in C Firon Software Development 4 11-05-2009 06:12 PM
Problem with a foreach loop in PHP JiJi Software Development 2 20-11-2008 05:57 PM
Problem - Outlook 2007 stops working under Vista Y.A Vista Help 18 26-06-2008 06:30 AM


All times are GMT +5.5. The time now is 04:17 PM.