LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 08-07-2010, 07:27 AM   #1
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Rep: Reputation: 35
ping in a script reporting an error


Hi

I am trying to run a simple piece of code but is failing on the count of the ping. I want it to report only 1 ping to my remote server and if no ping then error with a message or else the rest of the code will proceed.

Its basically telling me that my rmt server is alive and ready to take connections etc etc.

Heres the code:

Quote:
DATE=`date +%Y/%m/%d`
TIME=`date |awk '{print $4}'`
RMT_HOST=Zeus
COUNT=1
PING=`ping -c ${COUNT} ${RMT_HOST} | grep "icmp_seq=1"`

## MAIN ##

if [[ ${PING} != 0 ]] ;then
echo "[${DATE} - ${TIME}] - ERROR: REMOTE STORAGE SERVER IS DOWN"
exit 8
fi
The error message I am getting at the end is:

Quote:

+ [ != 0 ]
[: 1: 0: unexpected operator
It's complaining about the count number I think.. but for the life of me cannot work out why.

Any suggestions much appreciated.

Thanks
 
Old 08-07-2010, 07:42 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,252

Rep: Reputation: 313Reputation: 313Reputation: 313Reputation: 313
For me it works. I think the error concern the line:
Code:
echo "[${DATE} - ${TIME}] - ERROR: REMOTE STORAGE SERVER IS DOWN"
and '[' and ']' characters. Try removing it or preceding with slash '\'. Maybe they have special meanings.
 
Old 08-07-2010, 07:46 AM   #3
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 5.4, Mac OS 10.4 (tiger)
Posts: 1,005

Rep: Reputation: 79
Could I point out you appear to have a logic error here... because as far as I can see, you are telling it to report the error if ping != 0. But if the ping is successful it won't equal 0 anyway... Else wise I can not see the exact error and copying and pasting does not replicate the error, I wonder if the issue might be the way "Zeus" is configured to point at whatever ip/domain. Have you tried doing it to the IP instead to check this is not the issue?

EDIT:

Bash scripting really is not my area but I believe below is a slightly better version of what you are doing. Naturally I do not have a domain called Zeus so I have replaced this with 127.0.0.1 for the testing purposes, changing it to say domain.com produces the echo statement correctly.

Code:
DATE=`date +%Y/%m/%d`
TIME=`date |awk '{print $4}'`
RMT_HOST=127.0.0.1
COUNT=1
PING=`ping -c ${COUNT} ${RMT_HOST} | grep "icmp_seq=1"`

## MAIN ##

if [[ ${PING} = "" ]] ;then
echo "[${DATE} - ${TIME}] - ERROR: REMOTE STORAGE SERVER IS DOWN"
exit 8
fi

Last edited by r3sistance; 08-07-2010 at 07:55 AM.
 
Old 08-07-2010, 08:08 AM   #4
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Original Poster
Rep: Reputation: 35
eSelix, thanks for the reply but it was not the echo string it was complaining about.

r3sistance, I understand that my logic went a bit weird then.. thanks for pointing it out. I have now used the code you have supplied and here is the output:

(with double brackets)
Quote:
+ set -x
+ date +%Y/%m/%d
+ DATE=2010/08/07
+ date
+ awk {print $4}
+ TIME=13:04:35
+ RMT_HOST=zeus
+ COUNT=1
+ ping -c 1 zeus
+ grep icmp_seq=1
+ PING=
+ [[ = ]]
Monitor-bkups.NEW.sh: 1: [[: not found

(Without double brackets)
Quote:
+ set -x
+ date +%Y/%m/%d
+ DATE=2010/08/07
+ date
+ awk {print $4}
+ TIME=13:03:43
+ RMT_HOST=zeus
+ COUNT=1
+ ping -c 1 zeus
+ grep icmp_seq=1
+ PING=
+ [ = ]
[: 1: unexpected operator
Code I am using (slightly changed)

Quote:
DATE=`date +%Y/%m/%d`
TIME=`date |awk '{print $4}'`
RMT_HOST=zeus
COUNT=1
PING=`ping -c ${COUNT} ${RMT_HOST} | grep "icmp_seq=1"`

## MAIN ##

if [[ ${PING} = "" ]] ;then
echo "ERROR: REMOTE STORAGE SERVER IS DOWN"
exit 8
fi
 
Old 08-07-2010, 08:13 AM   #5
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Original Poster
Rep: Reputation: 35
Running the following string works on the cmd line:

Quote:
ping -c 1 zeus |grep "icmp_seq=1"
If I turn ping response off I get an empty blank line and when turned on I get a single line from ping which is all what I need. Just doesnt seem to like it in the code for some reason.
 
Old 08-07-2010, 08:13 AM   #6
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 5.4, Mac OS 10.4 (tiger)
Posts: 1,005

Rep: Reputation: 79
I believe the issue is with 'RMT_HOST=zeus' did you try replacing zeus out for the domain name/ip as I suggested to see if this is the issue? Or try 'RMT_HOST="zeus"'?

Edit: PS is it zeus or Zeus? your first and previous responses use two different cases for the Z, if I am right this type of stuff is generally case sensitive.

Last edited by r3sistance; 08-07-2010 at 08:16 AM.
 
Old 08-07-2010, 08:23 AM   #7
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Original Poster
Rep: Reputation: 35
OK tried now with a domain instead of my server name.

Quote:

+ set -x
+ date +%Y/%m/%d
+ DATE=2010/08/07
+ date
+ awk {print $4}
+ TIME=13:22:28
+ RMT_HOST=www.google.co.uk
+ COUNT=1
+ ping -c 1 www.google.co.uk
+ grep icmp_seq=1
+ PING=64 bytes from 173.194.36.104: icmp_seq=1 ttl=57 time=53.2 ms
+ [ 64 bytes from 173.194.36.104: icmp_seq=1 ttl=57 time=53.2 ms = ]
[: 1: bytes: unexpected operator
Edit: It doesn't matter if its Zeus, zeus, ZEUS.. it still pings from the cmd line.

Rgds

Last edited by fusion1275; 08-07-2010 at 08:25 AM.
 
Old 08-07-2010, 08:27 AM   #8
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 5.4, Mac OS 10.4 (tiger)
Posts: 1,005

Rep: Reputation: 79
Hmmm, I am not certain on this one. It seems to work for me... could you run the command "ls -l /bin/sh" and tell me what it returns. Generally looking for it's returning something like bash, dash, etc.

EG

lrwxrwxrwx 1 root root 4 Apr 12 2009 /bin/sh -> bash

It's the "/bin/sh -> <shell>" that would be important.

Last edited by r3sistance; 08-07-2010 at 08:29 AM.
 
Old 08-07-2010, 08:36 AM   #9
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Original Poster
Rep: Reputation: 35
I was expecting bash but hey.....

Quote:
lrwxrwxrwx 1 root root 4 2008-05-08 21:39 /bin/sh -> dash
 
Old 08-07-2010, 08:38 AM   #10
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 5.4, Mac OS 10.4 (tiger)
Posts: 1,005

Rep: Reputation: 79
I presume you are likely using either Debian or Ubuntu

Try placing as the first line of the script

#!/bin/bash
(this is called a shebang/hashbang for reference, wikipedia)

Alternatively try running the script by doing "bash <scriptname>" instead of presumably "sh <scriptname>" as I am guessing you are doing.

Last edited by r3sistance; 08-07-2010 at 08:41 AM.
 
Old 08-07-2010, 08:40 AM   #11
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Original Poster
Rep: Reputation: 35
Still playing with the script and also changed the shell to bash...

Quote:

+ set -x
+ date +%Y/%m/%d
+ DATE=2010/08/07
+ date
+ awk {print $4}
+ TIME=13:38:18
+ RMT_HOST=www.google.co.uk
+ COUNT=1
+ ping -c 1 zeus
+ awk {print $6}
+ grep icmp_seq=1
+ PING=
+ [ = ]
[: 1: unexpected operator

Quote:
home:/usr/local/bin# echo $SHELL
/bin/bash
 
Old 08-07-2010, 08:40 AM   #12
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
Grep greps the while line. You try to compare a sentence with an empty string.

Code:
if [ "$PING" == "" ]
should solve that.

jlinkels
 
Old 08-07-2010, 08:41 AM   #13
fusion1275
Member
 
Registered: Jul 2007
Location: Portsmouth, Hants
Distribution: Linux Mint
Posts: 266

Original Poster
Rep: Reputation: 35
I always have the shebang on all of my scripts. Never leave home without it
 
Old 08-07-2010, 08:42 AM   #14
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,252

Rep: Reputation: 313Reputation: 313Reputation: 313Reputation: 313
There is also better method for checking if host is alive
Code:
ping -c ${COUNT} ${RMT_HOST}
PING=$?
and later checking
Code:
if [[ "$PING" != "0" ]] ;then

Last edited by eSelix; 08-07-2010 at 08:44 AM.
 
Old 08-07-2010, 08:45 AM   #15
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 5.4, Mac OS 10.4 (tiger)
Posts: 1,005

Rep: Reputation: 79
Quote:
Originally Posted by fusion1275 View Post
changed the shell to bash...
I do not believe this is recommended as some ubuntu and debian scripts are specifically designed for dash, the reason for things like shebangs is to enforce the script as such that it works with the intended shell environment.
 
  


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
Spam reporting script was working now fails Toadman Programming 8 12-09-2009 11:25 AM
SMB Error : Kernal Reporting smb_get_length: Invalid NBT packet ,code=fd ukn_linux Linux - Newbie 1 05-10-2008 04:49 PM
LXer: UNIX error reporting in the standard C library LXer Syndicated Linux News 0 09-08-2006 05:33 AM
Error reporting in c george_mercury Programming 2 11-20-2004 07:22 PM
mail log reporting error every minute kwingate Fedora 0 10-25-2004 10:07 PM


All times are GMT -5. The time now is 02:18 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