[X-Unix] shell script - online or not?

Wing Wong wingedpower at gmail.com
Fri Jun 2 09:54:34 PDT 2006


On 6/2/06, Charles Howse <chowse at charter.net> wrote:

> What might I be doing wrong?

Short answer? Ping's error output needs to be redirected to standard out:

ping ..... 2>&1 | grep ....

Also need to take into account when the logfile doesn't exist.

Longer answer:

In your code, the data tested and the data written would not be the
same since you are trying to run the same test again when you write to
the file. Included below is a variation of your script. You can change
the logfile value at the start of the script instead of having to
change every instance of it throughout the script.

Tail command using the short parameter version.

Ping's output redirected.

Logfile presence checked beforehand.


#!/bin/sh
host=24.159.64.20
logfile=ping_results.txt

if [ ! -f $logfile ]; then
      echo "$logfile either doesn't exist or isn't a file! Please check!"
      exit 1
fi

test=`ping -c 5 $host 2>&1 | grep packets`
look=`tail -1 ping_results.txt`

if [ "$test" == "$look" ] ; then
        :
else
        echo >> $logfile
        date >> $logfile
        echo "$test" >> $logfile
fi

exit 0


-- 
Wing Wong
wingedpower at gmail.com


More information about the X-Unix mailing list