0

I don't know the best way to form this question, but here's the code I want to work:

case $MESSAGE in
    ?Trcx!~*PRIVMSG*[${Nick}|${AltNick}]*[nN][mM]ap*)
       echo "Some Code";;
     *)
       echo "Some Other Code";;
esac

How would I make it work? The part that is not working is the [${Nick}|${AltNick}] I want the expression to be valid for both the $Nick and $AltNick var. Any help is greatly appreciated!

4

3 回答 3

2

Bash glob patterns are documented here. You want this: @(patt1|patt2). Note the extglob shell option must be enabled.

shopt -s extglob
case $MESSAGE in
    ?Trcx!~*PRIVMSG*@(${Nick}|${AltNick})*[nN][mM]ap*)
       echo "Some Code";;
     *)
       echo "Some Other Code";;
esac
于 2011-10-20T03:13:48.850 回答
2

In glob patterns, without Bash's "extglob" feature, you cannot group a subpattern with parentheses - and certainly not with square brackets. The Bourne-compatible way is to split into two distinct patterns.

case $MESSAGE in
  ?Trcx!~*PRIVMSG*${Nick}*[nN][mM]ap* |
  ?Trcx!~*PRIVMSG*${AltNick}*[nN][mM]ap*)
    echo "Some Code";;
  *)
    echo "Some Other Code";;
esac
于 2011-10-20T04:46:46.533 回答
1

Bash performs case pattern matching just like filename matching, not regular expression matching. If that's what you want, then problem solved.

However, if you want a regular expression match, perhaps you could try the [[ expression ]] compound command to achieve your goals, which offers the =~ operator for regular expression matching.

#!/bin/bash

regexp="^[a-z]*$"
if [[ $1 =~ $regexp ]] ; then                                                                                                                                                                   
   echo "match"
else
   echo "no match"
fi
于 2011-10-20T03:31:10.977 回答