Yes, the
exit commands will break the script at that point, as long as the conditions match.
Probably the best you can do is save a "status" variable and exit with that at the end. It will only show the status of the last loop iteration though, unless you write something that stores and prints the outputs of all the drives individually.
I would also change the script to use a single
array instead of a brace expansion. Much cleaner. It would also be more efficient to store the output of
df once, and parse that for the values you want. You can then use
parameter substitutions to extract them more efficiently than with awk/sed/grep. The fewer calls to external tools the better, usually.
Finally, when using
bash or
ksh, it's recommended to use
[[..]] for string/file tests, and
((..)) for numerical tests. Avoid using the old
[..] test unless you specifically need POSIX-style portability.
http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
Here's the modified version I came up with:
Code:
#!/bin/bash
volumes=( '/disk0' '/global/disk01_01' '/global/disk02_01' '/global/backup' )
critspace=20
warnspace=40
critpercent=95
warnpercent=90
for i in ${volumes[@]}"; do
echo "Processing drive $i"
#store the output of df in an array
dfoutput=( $( df -h "$i" | cut -d $'\n' -f2 ) )
#extract and format the desired fields and echo the results
spacefree=${dfoutput[3]%G}
percentcapacity=${dfoutput[4]%\%}
echo "spacefree $spacefree"
echo "percentfree $percentcapacity"
#compare current stats to critical values
if (( spacefree <= critspace && percentcapacity >= critpercent )); then
echo "NOK $spacefree free and $percentcapacity user on $i"
status=2
elif (( spacefree <= warnspace && percentcapacity >= warnpercent )); then
echo "WARNING $spacefree free and $percentcapacity user on $i"
status=2
else
echo "ALL OK"
fi
done
exit "${status:-0}"
Note that this simply sets the script's exit status to "2" if any of the devices turn up with warnings, which is what I imagine you'd want to happen. Again, more detailed error messages would require extra work.
Notice also how I stored the output of
df into an array too (after using
cut to chop off the first line), making it easier to access the individual fields.
Actually we could probably even skip using
cut and just change it to extract fields 10 and 11 instead.