not – integer number comparison

Some day ago I needed a shell script to compare some decimal values, but it’s not possible to compare as usual

MAX=33.33
if [ $MYVALUE -gt $MAX ]; then ….

Running something like this will bring the error message “integer expression expected!”.

With a little help from bc, it’s possible to compare non decimal values. Here follows a simple unuseful nagios plugin to compare decimal values:

#!/bin/bash

MYVALUE=$1
MAXWARN=”82.5″
MAXCRIT=”91.3″
MYDESCR=”Value description”
RET_OK=”0″
RET_WARN=”1″
RET_CRIT=”2″
RET_UNKN=”3″

PERFDATAMSG=”|’$MYDESCR’=$MYVALUE;$MAXWARN;$MAXCRIT”

if [ $(echo “$MYVALUE > $MAXCRIT”|bc) -gt 0 ]; then
echo “ERROR – $MYDESCR=$MYVALUE$PERFDATAMSG”
exit $RET_CRIT
fi

if [ $(echo “$MYVALUE > $MAXWARN”|bc) -gt 0 ]; then
echo “WARNING – $MYDESCR=$MYVALUE$PERFDATAMSG”
exit $RET_WARN
else
echo “OK – $MYDESCR=$MYVALUE$PERFDATAMSG”
exit $RET_OK
fi

Tags:

Leave a Reply