[clue-tech] shell globbing and case

marcus hall marcus at tuells.org
Tue Mar 2 09:08:31 MST 2010


On Mon, 2010-03-01 at 16:52 -0700, David L. Willson wrote:
> I'm writing a Nagios plug-in, and adhering to the developer guide, and writing in bash script.
> According to the book, I should support threshold statements like
> 
> 42
> 11:
> :42
> 11:42
> 
> where my script alerts when the thing being measured is
> 
> above 42
> below 11
> above 42
> above 42 or below 11
> 
> The bash "case" statement accepts some pretty wild things.  As I
> understand it, it's behavior mimics the capabilities of bash's
> file-selection wildcards.  I just learned, for example, that you can
> use character-classes like [[:digit:]] and [[:alpha:]] with both.
> 
> Does anyone have a recommendation for an on-line or local resource with a good discussion of the the capabilities?  It's not regular regex, but it's a Heck of a lot more than just * and ?, too.
> 

Although shell glob patterns are fairly expressive, they do not have the
power of full regular expression parsing.  It has a hard time recognizing
"all numerics".  But, if you know it's one or two digits, you could
match things with:

case "$i" in
[:digit:]|[:digit:][:digit:])
[:digit:]:|[:digit:][:digit:]:)
:[:digit:]|:[:digit:][:digit:])
[:digit:]:[:digit:]|[:digit:]:[:digit:][:digit:]|[:digit:][:digit:]:[:digit:]|[:digit:][:digit:]:[:digit:][:digit:])
esac

If you need tor recognize unlimited digits, you may be better off with expr:

above=${i#*:}
case "$i" in
"")	die "Empty expression";;
*:)	above=${i#*:} below=;;
:*)	above= below=${i%:*};;
*:*)	above=${i#*:} below=${i%:*};;
*)	above="$i";;
esac
expr>/dev/null "$above" : '[0-9]*' || die 'Bad value for above'
expr>/dev/null "$below" : '[0-9]*' || die 'Bad value for below'

marcus hall
marcus at tuells.org


More information about the clue-tech mailing list