Sunday, February 14, 2010

bash and floating point numbers

In short: bash does not do arithmetic on floating point numbers. If I was doing anything serious with these numbers, I guess it'd be time to re-do this in perl.

But in this case, all I want to do is see if someone is using more than 90% of their quota, so I'm happy to round down to the nearest integer. Witness my hack (where PC is the percentage of quota used with 2 decimal places, and IPC is the integer representation of this after rounding down):


IPC=$(echo "$PC /1" | bc)
if (( IPC > WARN_THRESH ))
then
echo "WARN $USER they are using $PC % of their $QUOTA quota"
fi


Ugly, but it works.

No comments:

Post a Comment