LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-02-2003, 05:14 PM   #1
Satriani
Member
 
Registered: Mar 2003
Location: The Netherlands
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418

Rep: Reputation: 30
Basic BASH script, what's wrong???


Hi,

I'm trying to write a very simple shellscript, but somehow it does exactly the opposite of what I expected.
I want to run a script, but only if a particular server in my network is reachable.. So here's my script:
(note that the IP address does not exist in my network...)
-------------------------------------------------------------------------
#!/bin/bash

pingresult=( `ping -w 3 192.168.1.100 |grep -i unreachable` )

if [ -z $pingresult ]
then
echo "Network-machine is not reachable"
else
echo "Yup, the machine replied"
fi
----------------------------------------------------------------------------

I thought that the test -z checked for an empty string.
So, if $pingresult is empty, the test -z should return true, thus it should echo Network-machine not reachable.
However, it echos the Yup ... thing

When I use an existing IP address, it echo's the: Network-machine not reachable..

Shouldn't it be the other way around? Am I missing something here, or did I do something wrong???

(Or if anyone knows a better way to see if a machine can be reached through the network, please feel free to share it with me )
 
Old 06-02-2003, 06:21 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,360
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
ping -w 3 192.168.1.100 | grep -qi unreachable; case $? in
0) echo "Network-machine is not reachable";;
1) echo "Yup, the machine replied";;
esac

What you did with pingresult is make an array you're not using ("echo ${pingresult[@]}"). Then you're not testing for the term "Unreachable" let alone use it. Besides, you do not need to work with the text, a quiet grep for the term would do, right? Exit status of grep provides enough info. Always try to use "set -x" to troubleshoot scripts, or execute as "sh -x /some/script".

If you went to Freshmeat you'd find clients for what you want to do, if you want, what I'd call the swiss armyknife amongst them, check out "Monit" it can check hosts, and much more.
 
Old 06-02-2003, 06:34 PM   #3
Satriani
Member
 
Registered: Mar 2003
Location: The Netherlands
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418

Original Poster
Rep: Reputation: 30
Thanks unSpawn!

I do not completely understand your answer, but I do get where you go with you example.
What I want to do eventually is, when the machine can be pinged (thus is reachable) run another script to start some apps
If its not there, then just quit this script...
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic bash script question kevpatts Linux - Newbie 3 10-12-2005 02:02 PM
Basic Bash script help valadamir Programming 2 12-01-2004 11:37 AM
Trouble with a basic bash script Tengil Linux - Newbie 5 03-04-2004 01:59 PM
My first BASH script, what's wrong with it? szf2 Linux - Newbie 2 11-12-2003 02:43 PM
help with basic syntax in bash script Supp0rtLinux Linux - Software 4 03-27-2003 07:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:27 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration