[HPADM] SUMMARY: ksh test for pattern match ??

From: Abramson, Stuart (SAbramson@Wabtec.com)
Date: Thu Oct 02 2003 - 14:11:37 EDT


Thanks to:

        Allan Marillier
        David Totsch

David's answer below.

        Stuart

Stuart Abramson | Off: 412/825-1434 | Cell: 412/417-1567 | email:
sabramson@wabtec.com

-----Original Message-----
From: TOTSCH,DAVID (HP-USA,ex3) [mailto:david.totsch@hp.com]
Sent: Thursday, October 02, 2003 1:22 PM
To: Abramson, Stuart
Subject: RE: [HPADM] ksh test for pattern match ??

Stuart:

if you want the shell to make the decision (not all another program like
grep, awk, expr), you might resort to 'case'

MATCH=""
case $SID_DG in
bkx_al) MATCH=YES ;;
bkx_db) MATCH=YES ;;
bkx_tp) MATCH=YES ;;
rpx_al) MATCH=YES ;;
rpx_db) MATCH=YES ;;
rpx_tp) MATCH=YES ;;
esac

if [[ -n $MATCH ]]
then
        # YOU HAVE A MATCH
fi

Knowing that 'case' matches against file name generation

MATCH=""
case $SID_DG in
bkx_[adt][lbp]) MATCH=YES ;;
rpx_[adt][lbp]) MATCH=YES ;;
esac

but that hard to read and allows bkx_ap, etc.

There is an alternation syntax for 'case':

MATCH=""
case $SID_DG in
bkx_al|bkx_db|bkx_tp|rpx_al|rpx_db|rpx_tp) MATCH=YES ;;
esac

But that on is difficult to modify (add/delete matches).

Korn and POSIX shells allow additional file name generation
syntax, making the following possible:

MATCH=""
case $SID_DG in
bkx_@(al|db|tp) MATCH=YES ;;
rpx_@(al|db|tp) MATCH=YES ;;
esac

or you could shorten to

MATCH=""
case $SID_DG in
@(bkx|rpx)_@(al|db|tp) MATCH=YES ;;
esac

However, for future modifiability, I like the ability
to create a file with the list of allowable matches
(lets say that file is pointed to by VALID_MATCHES):

if print $SID_DG | grep -f ${VALID_MATCHES} -q
then
        #match found
fi

Be sure that your list in VALID_MATCHES uses regular expressions
to match exact entries (eg ^bkx_al$ ).

Or, if you want the list of valid matches to be in the script

if grep -q "^${SID_DG}$"
then
        MATCH=YES
fi <<- End-of-List
        bkx_al
        bkx_db
        bkx_tp
        rpx_al
        rpx_db
        rpx_tp
        End-of-List

In the above, "<<-" is special in that the dash tells the shell
to remove leading tabs.

Enjoy,
-dlt-

> -----Original Message-----
> From: Abramson, Stuart [mailto:SAbramson@Wabtec.com]
> Sent: Thursday, October 02, 2003 9:47 AM
> To: Hpux-Admin (E-mail)
> Subject: [HPADM] ksh test for pattern match ??
>
>
> hpux-admins:
>
> Is there a better way to do this?
>
> I don't want any partial matches. Only exact matches to one case.
>
> If there is a match, I process a bunch of stuff.
>
> If there is no match, I exit with message.
>
> Stuart
>
> =============================================
>
>
> SID_DG=$1
> VALID_CASES=" bkx_al bkx_db bkx_tp
> rpx_al rpx_db rpx_tp"
> MATCH="NO"
>
> # Check for input request case match
>
> for CASE in $( echo $VALID_CASES )
> do
> if [[ $SID_DG = $CASE ]]
> then
> # We have a valid establish request
> MATCH="YES"
> fi
> done
>
> Stuart Abramson | Off: 412/825-1434 | Cell: 412/417-1567 | email:
> sabramson@wabtec.com
>
>
>
> ***** CONFIDENTIALITY NOTE *****
> The content contained in this e-mail transmission is legally
> privileged and confidential information intended only for the
> use of the individual or entity named herein. If the reader
> of this transmission is not the intended recipient, you are
> hereby notified that any dissemination, distribution, or
> copying of this transmission is strictly prohibited.
>
> --
> ---> Please post QUESTIONS and SUMMARIES only!! <---
> To subscribe/unsubscribe to this list, contact
> majordomo@dutchworks.nl
> Name: hpux-admin@dutchworks.nl Owner:
> owner-hpux-admin@dutchworks.nl
>
> Archives: ftp.dutchworks.nl:/pub/digests/hpux-admin
> (FTP, browse only)
> http://www.dutchworks.nl/htbin/hpsysadmin (Web,
> browse & search)
>

***** CONFIDENTIALITY NOTE *****
The content contained in this e-mail transmission is legally privileged and confidential information intended only for the use of the individual or entity named herein. If the reader of this transmission is not the intended recipient, you are hereby notified that any dissemination, distribution, or copying of this transmission is strictly prohibited.

--
             ---> Please post QUESTIONS and SUMMARIES only!! <---
        To subscribe/unsubscribe to this list, contact majordomo@dutchworks.nl
       Name: hpux-admin@dutchworks.nl     Owner: owner-hpux-admin@dutchworks.nl
 
 Archives:  ftp.dutchworks.nl:/pub/digests/hpux-admin       (FTP, browse only)
            http://www.dutchworks.nl/htbin/hpsysadmin   (Web, browse & search)


This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 11:02:34 EDT