Results 1 to 7 of 7

Thread: Bash script failing (detect if ping successful)

  1. #1
    Join Date
    Apr 2009
    Beans
    264
    Distro
    Ubuntu 12.04 Precise Pangolin

    Red face [solved] Bash script failing (detect if ping successful)

    I've been making a bash script that involves checking if it can ping. I swear it was working before, but now it throws On or Off depending on if I commend or uncomment the first line here.

    The purpose of the while loop is to keep looping until it can successfully ping.

    Code:
    #Internet=0
    while [ ! $Internet ]
      do
      ping -c 1 -w 3 google.com
      if [ $? -ne  ] ; then
        $Internet = 0
        echo Off
      else
        $Internet = 1
        echo On
      fi
      #More code.
    done
    Last edited by Muscovy; May 24th, 2010 at 11:09 PM.

  2. #2
    Join Date
    Jun 2008
    Location
    Ballard
    Beans
    2,409
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Bash script failing (detect if ping successful)

    Try setting the first line to Internet= (that's right, nothing). Then what is the output?
    "We're all in this together, kid." --H. Tuttle (a.k.a. H. Buttle)
    "Maybe it's a layer 8 problem." --thatguruguy
    A High-Tech Blech!

  3. #3
    Join Date
    Apr 2009
    Beans
    264
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Bash script failing (detect if ping successful)

    The definition isn't skipping the while loop any more, but pings are still always returned as true.

  4. #4
    Join Date
    Apr 2009
    Beans
    264
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Bash script failing (detect if ping successful)

    Also, I've been getting the following errors.
    Code:
    /usr/bin/alexandos-stats: line 7: [: 2: unary operator expected
    /usr/bin/alexandos-stats: line 11: =: command not found
    Which I read up on as "non fatal", and doing the recommended solution of wrapping the problem code in quotes makes it always print Off.

  5. #5
    Join Date
    Jun 2008
    Location
    Ballard
    Beans
    2,409
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Bash script failing (detect if ping successful)

    I can't make sense of those errors counting your line numbers. Is that the entire script?
    "We're all in this together, kid." --H. Tuttle (a.k.a. H. Buttle)
    "Maybe it's a layer 8 problem." --thatguruguy
    A High-Tech Blech!

  6. #6
    Join Date
    Apr 2009
    Beans
    264
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Bash script failing (detect if ping successful)

    Whoops. Found it.
    Code:
      if [ $? -ne 0 ] ; then
    I accidentally deleted the value it was looking for in the returned data from pinging.

  7. #7
    Join Date
    Jun 2008
    Location
    Ballard
    Beans
    2,409
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Bash script failing (detect if ping successful)

    Doh! Great news. Mark your thread as SOLVED.
    "We're all in this together, kid." --H. Tuttle (a.k.a. H. Buttle)
    "Maybe it's a layer 8 problem." --thatguruguy
    A High-Tech Blech!

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
  •