LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 11-15-2009, 03:45 AM   #1
jcky
Member
 
Registered: Feb 2008
Location: China-Shanghai
Distribution: Rhel&Suse
Posts: 82

Rep: Reputation: 15
Bash Script


#!/bin/bash
# alive2.sh
# Checks to see if hosts 192.168.1.100-192.168.1.105 are alive
# Iterate through IP address
for n in {100..105};
do
host=192.168.1.$n
ping -c2 $host &> /dev/null
if [ $? = 0 ];
then
echo "$host is UP"
else
echo "$host is DOWN"
fi
done

What is the purpose of "if [ $? = 0 ];"?

I need your kind help.

;p
 
Old 11-15-2009, 03:55 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
"$?" is the return code from a command. "0" means the command executed successfully.

You could also do this:

Code:
if $(ping -c2 $host &> /dev/null); then
     do stuff
else
     do other stuff
fi

(Always use [CODE] tags to make code easier to read.)

Last edited by pixellany; 11-15-2009 at 04:00 AM.
 
Old 11-16-2009, 10:46 PM   #3
stevenworr
LQ Newbie
 
Registered: Oct 2008
Posts: 13

Rep: Reputation: 1
I can't resist:

Code:
for n in {100..105}
do
    host=192.168.1.$n
    echo -n "$host is "
    ping -c2 $host > /dev/null 2>&1 && echo up || echo down
fi
done
 
Old 11-17-2009, 06:54 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by stevenworr View Post
I can't resist:

Code:
for n in {100..105}
do
    host=192.168.1.$n
    echo -n "$host is "
    ping -c2 $host > /dev/null 2>&1 && echo up || echo down
fi
done
Nice!! (but take out the "fi")
 
  


Reply



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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 10:53 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:43 AM.

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
Open Source Consulting | Domain Registration