LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This 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
 
Thread Tools
Old 11-15-2009, 04:45 AM   #1
jcky
Member
 
Registered: Feb 2008
Location: China-Shanghai
Distribution: Rhel&Suse
Posts: 45
Thanked: 0
Bash Script


[Log in to get rid of this advertisement]
#!/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
windows_98_nt_2000 jcky is offline     Reply With Quote
Old 11-15-2009, 04:55 AM   #2
pixellany
Moderator
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch
Posts: 13,173
Thanked: 285
"$?" 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 05:00 AM..
linux pixellany is offline     Reply With Quote


Old 11-16-2009, 11:46 PM   #3
stevenworr
LQ Newbie
 
Registered: Oct 2008
Posts: 9
Thanked: 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
linux stevenworr is offline     Reply With Quote


Old 11-17-2009, 07:54 AM   #4
pixellany
Moderator
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch
Posts: 13,173
Thanked: 285
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")
linux pixellany is offline     Reply With Quote



Reply

Bookmarks


Thread Tools

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


All times are GMT -5. The time now is 12:39 AM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration