shell regex matching help
Chris F.A. Johnson
cfaj-uVmiyxGBW52XDw4h08c5KA at public.gmane.org
Tue Dec 12 21:50:52 UTC 2006
On Tue, 12 Dec 2006, Neil Watson wrote:
> I need help with this script.
>
> #!/bin/bash
>
> HOSTNAME=`hostname`
> SERVICE="db2"
> CLUSTAT=`/usr/sbin/clustat -s ${SERVICE}|tail -n1`
> # Should return:
> #db2 your-hostname started
>
> echo $HOSTNAME
> echo $SERVICE
> echo $CLUSTAT
>
> if [[ ${CLUSTAT} = ${SERVICE}[:blank:]+${HOSTNAME}[:blank:]+started ]] ; then
if [[ ${CLUSTAT} == ${SERVICE}[:blank:]+${HOSTNAME}[:blank:]+started ]] ; then
I'd avoid the non-standard [[...]] syntax and use case:
case $CLUSTAT in
"$SERVICE"*"$HOSTNAME"*started) echo "Node is active";;
*) echo "Node is not active"; exit 255 ;;
esac
> echo "Node is active"
> exit 0;
> fi
>
> # Else node is not active.
> echo "Node is not active"
> exit 255
>
> Output:
> : !./activenode
> tor-lx-hadrian
> db2
> db2 tor-lx-hadrian started
> Node is not active
>
> Why is the match failing?
Wrong operator.
man bash:
[[ expression ]]
...
When the == and != operators are used, the string to the right
of the operator is considered a pattern and matched according to
the rules described below under Pattern Matching. The return
value is 0 if the string matches or does not match the pattern,
respectively, and 1 otherwise. Any part of the pattern may be
quoted to force it to be matched as a string.
An additional binary operator, =~, is available, with the same
precedence as == and !=. When it is used, the string to the
right of the operator is considered an extended regular expres-
sion and matched accordingly (as in regex(3)). The return value
is 0 if the string matches the pattern, and 1 otherwise. If the
regular expression is syntactically incorrect, the conditional
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
===================================================================
Author:
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
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