shell regex matching help

Neil Watson tlug-neil-8agRmHhQ+n2CxnSzwYWP7Q at public.gmane.org
Tue Dec 12 21:52:49 UTC 2006


On Tue, Dec 12, 2006 at 04:22:31PM -0500, Giles Orr wrote:
>I'm not as good at BASH programming as I ought to be, so my preferred
>solution is dictated by limited knowledge ....
>
>I don't understand the above "if ..." line.  Wouldn't it be easier to
>use ${CLUSTAT##* } and see if that matches "started" rather than
>trying to reassemble the correct output?  If you need to check
>$SERVICE or $HOSTNAME because you're checking several services or
>hosts, perhaps check them separately too with similar name mangling?

Actually the problem was with the [:blank:] clause.  It should be
[[:blank:]].  The correct script is:

#!/bin/bash

HOSTNAME=`hostname`
SERVICE="db2"
CLUSTAT=`/usr/sbin/clustat -s ${SERVICE}|tail -n1`
# Should return: 
  #db2                  your-hostname                 started         

  if [[ ${CLUSTAT} =~ ${SERVICE}[[:blank:]]+${HOSTNAME}[[:blank:]]+started ]] ; then
      exit 0;
  fi

# Else node is not active.
exit 255

The node is active if the exit status ($?) is 0.

Example usage:
[nhwatson at tor-lx-hadrian]activenode && echo "active"

The echo command will be executed only if the node is active.  "&&"
means execute next command only if the previous command exits 0.

Another example: (untested)

#!/bin/bash

/usr/local/bin/activenode

if [[ $? = 0 ]]; then
        #script commands here
fi


-- 
Neil Watson             | Debian Linux
System Administrator    | Uptime 38 days
http://watson-wilson.ca
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list