I was writing a plugin for Nagios to check the number of security packages and doing a case statement as the one here.
but the whole thing ended up turning into a freak show. All I basically wanted to do was saying if there are between 1-20 packages show this warning, if 21-infinity show critical and so on, but I could not just do [ -gt 1 ]
or even [20-10000]
When applying these ranges the script would run kind of irregularly and warn when it was meant to be critical, etc. I think I gathered that ranges are not really possible here, I am just wondering why it worked for the example given above but not for me. Also, Is it in any way possible to include conditions within case?
In the end I managed to do a fully working script but I had to create if conditions and pass them to case. Just wondering if there was an easier way to do this without having to do that.
Below is more or less what I was trying to do. The last part was just me being desperate trying loads of different ranges to see if it worked.
case $SECURITY_PACKAGES in
0)
echo "OK - not bad: There are a total of $TOTAL_PACKAGES packages to upgrade in this server, but none of them are security updates!"
exit 0
;;
[1-20]*)
echo "WARNING - $TOTAL_PACKAGES packages required to upgrade in this server, of which $SECURITY_PACKAGES are security updates"
exit 1
;;
[21-99]|[100-999]|[1000-9999]*)
echo "CRITICAL - $SECURITY_PACKAGES out of $TOTAL_PACKAGES are security updates! Consider upgrading soon!"
exit 2
;;
*) echo "UNKNOWN - I am not sure what's happening now, check later or check server: $TOTAL_PACKAGES to upgrade, $SECURITY_PACKAGES are security updates"
exit 3
;;
esac
Best Answer
You cannot express number ranges easily in the
case
expressions - the pattern[1000-9999]
, for example, does not mean the numbers1000
to9999
, but the characters1
,0
,0
, the range0-9
, the characters9
,9
,9
- essentially all the digits.[1-85]
does not mean the numbers1
to85
, but the digits from1
to8
, and5
, ... which are just the digits from1
to8
. So[1-20]*
means anything that begins with1
,2
, or0
- so even20000000
will match that. Useif
/then
/elif
/else
/fi
instead: