Results 1 to 4 of 4

Thread: Problem with while loop that never stops

  1. #1
    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?

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    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

  3. #3
    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.

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    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).

Similar Threads

  1. Watercooling: Single loop or Dual Loop
    By Akolekar in forum Hardware Peripherals
    Replies: 3
    Last Post: 21-10-2011, 10:52 PM
  2. Game loop problem with XNA
    By Botan in forum Software Development
    Replies: 5
    Last Post: 30-11-2010, 05:35 AM
  3. Problem of infinite loop in C
    By Firon in forum Software Development
    Replies: 4
    Last Post: 11-05-2009, 06:12 PM
  4. Problem with a foreach loop in PHP
    By JiJi in forum Software Development
    Replies: 2
    Last Post: 20-11-2008, 05:57 PM
  5. Replies: 18
    Last Post: 26-06-2008, 06:30 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,727,019,552.36976 seconds with 17 queries