LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-29-2015, 07:29 PM   #1
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 514

Rep: Reputation: 48
simple ping bash script


Hello, I am trying to implement this script, but for some reason, it limits to pinging 192.1681.1 and then 192.168.1.2 infinitely.
Any idea what is wrong with it? Thanks!

Quote:
PREFIX=192.168.1
OCTET=1
while [ "$OCTET" -lt "255" ]; do
echo -en "Pinging ${PREFIX}.${OCTET}..."
ping -c1 -w1 ${PREFIX}.${OCTET}
if [ "$?" -eq "0" ]; then
echo "OK"
else
echo "Failed"
let OCTET=$OCTET+1
fi
done
 
Old 05-29-2015, 07:33 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,403

Rep: Reputation: Disabled
Allow me to reformat slightly and add a "shebang" at the top:
Code:
#!/bin/sh

PREFIX=192.168.1
OCTET=1

while [ "$OCTET" -lt "255" ]; do
  echo -en "Pinging ${PREFIX}.${OCTET}..."
  ping -c1 -w1 ${PREFIX}.${OCTET}
  if [ "$?" -eq "0" ]; then
    echo "OK"
  else
    echo "Failed"
    let OCTET=$OCTET+1
  fi
done
Can you see what the problem is now?
 
Old 05-29-2015, 07:41 PM   #3
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 514

Original Poster
Rep: Reputation: 48
Quote:
Originally Posted by Ser Olmy View Post
Allow me to reformat slightly and add a "shebang" at the top:
Code:
#!/bin/sh

PREFIX=192.168.1
OCTET=1

while [ "$OCTET" -lt "255" ]; do
  echo -en "Pinging ${PREFIX}.${OCTET}..."
  ping -c1 -w1 ${PREFIX}.${OCTET}
  if [ "$?" -eq "0" ]; then
    echo "OK"
  else
    echo "Failed"
    let OCTET=$OCTET+1
  fi
done
Can you see what the problem is now?
Ah. the "let" statement is in the "if", instead of "while".

Thanks!
 
Old 05-29-2015, 07:46 PM   #4
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,403

Rep: Reputation: Disabled
Quote:
Originally Posted by vincix View Post
Ah. the "let" statement is in the "if", instead of "while".
Indentation makes such errors much easier to spot.
Quote:
Originally Posted by vincix View Post
Thanks!
You're welcome!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash while read loop ping script - help? diamond_D Linux - General 10 07-31-2014 07:58 AM
Bash ping script SirTonic Linux - Networking 12 05-20-2013 03:54 PM
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 10:46 AM
Simple...ish ping script. hypercondor Linux - Newbie 5 05-09-2008 12:08 AM
Simple bash script help zok Linux - General 4 12-15-2006 12:56 PM


All times are GMT -5. The time now is 08:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration