Alert on partition growth I have a script that alerts me via email when a partition grows past a defined threshold in one hour. My script is run from cron hourly. Everything seems fine for 99% of the time, but occasionally I get an email from my script stating that growth in the past hour is something like ".08%", well below my current threshold of 0.75%. Is something wrong with my logic?

#Drive to monitor
monDrive=/opt
#Enter a number .01-100. i.e 50 = 50%, and 0.5 = 1/2 of a percent.
percentThreshold=0.75
#Email address to send alerts to
emailAlert=alerts@domain.com


##Do not modify anything below this point
prevReading=/root/diskcheckreading.txt



checkDiskUse() {
        df "$monDrive" | awk 'END{printf "%0.2f",$2/$1*100; exit}'
}

checkDiskFree() {
        df "$monDrive" | awk 'END{printf "%0.2f",$3/1048576; exit}'
}

#Check if file exists with previous reading
[ -f $prevReading ] &amp;&amp; prev=$(cat $prevReading) || { checkDiskUse &gt; $prevReading; exit; }

#Validate file content
printf "%0.2f" "$prev" &gt; /dev/null || exit

#Get current percentage used of monitored drive
current=$(checkDiskUse)

#Get current free capacity of monitored drive
diskFree=$(checkDiskFree)

#Calculate the difference
results=$(bc -l &lt;&lt;&lt; $current-$prev )
#Check if the difference exceeds the threshold
if  [[ $results &gt; $percentThreshold ]]
         then printf "\"$monDrive\" has grown $results%% in the past hour.     Remaining capacity on $monDrive is $diskFree GB.\n\n$(hostname):$(pwd)/$(basename $0)" | mail -s "Disk Alert - $(hostname):$monDrive" $emailAlert
fi

#log current reading for next check
echo $current &gt; $prevReading
Everything seems fine for 99% of the time, but occasionally I get an
  email from my script stating that growth in the past hour is something
  like ".08%", well below my current threshold of 0.75%. Is something
  wrong with my logic?


When the tools seem to defy your logic, play with simple examples.

The code that generates the message with ".08%" is this, roughly:


printf "\"$monDrive\" has grown $results%% in the past hour. | mail ...



where the $results variable comes from:


results=$(bc -l &lt;&lt;&lt; $current-$prev )



and the threshold is checked with:


if  [[ $results &gt; $percentThreshold ]]



Now let's play a little:

$ [[ 0.08 &gt; 0.75 ]] &amp;&amp; echo true || echo false
false
$ [[ .08 &gt; 0.75 ]] &amp;&amp; echo true || echo false
true


A-ha! Apparently, 0.08 is smaller than 0.75, as expected but .08, with the leading zero omitted, is not smaller than 0.75, at least in Bash.

But why do you have .08 instead of 0.08 in the first place?
Let's play a bit more:

$ bc -l &lt;&lt;&lt; 2.81-0.79
2.02
$ bc -l &lt;&lt;&lt; 0.81-0.79
.02
$ bc -l &lt;&lt;&lt; 0-0.79
-.79


Ah. So when the result is below 1, apparently, bc drops the leading zero.

So how to fix this? It seems there's no obvious way to prevent bc from dropping the leading zero. But as you're already doing many calculations using awk, you can move this calculation there too:

results=$(awk '{print $1 - $2}' &lt;&lt;&lt; "$current $prev")


Note the space between $current and $prev, 
so that they can be used as $1 and $2 in awk.

Code review


   df "$monDrive" | awk 'END{printf "%0.2f",$2/$1*100; exit}'



In the END block of awk you don't need to exit,
as the command will exit anyway.
I also recommend to add more spaces to make the expression easier to read:

    df "$monDrive" | awk 'END { printf "%0.2f", $2 / $1 * 100 }'




This seems completely pointless, you can delete it:


#Validate file content
printf "%0.2f" "$prev" &gt; /dev/null || exit