I wrote I ‘quick’ script, and then thought of adding a “—nomail” option that would shut off my mail switch (of which the syntax is piped in from echo).
I thought this would be easy but I think, fundamentally, I did not structure the original script well so, this ‘option’ is killing me.
Very new to bash… THANKS!
MY WONDERFUL WORKING SCRIPT!:
## Current threshold value setting
THRESHOLD="80"
## Input Files and Mailx
LOCATIONS="fsIn.txt" ## Edit "fsIn.txt" to add or change filesystems
TOLIST="$(cat toList.txt | tr -s '\n' ' ' )" ## Sets who receives email if an error condition exists
REPLYTO="$(cat replyTo.txt | tr -s '\n' ' ' )" ## sets the reply to email addresses
FROM=”SCRIPT TEAM" ## Sets the "from" address on error emails
SUBJECT="$HOST: ! STORAGE LEVEL MET OR EXCEEDED !" ## Sets the subject line
############
for i in $(cat $LOCATIONS)
do
## Main df pipeline to return usage percentage
CAP=$(df -PH --no-sync "$i" | awk 'NR>1'| awk {'print $5'} | sed 's/.$//')
for i in $(cat $LOCATIONS) #Several different file system locations are ‘catted’ in here . E.g. /dev
do
## Main df pipeline to return usage percentage to stdout and piped to mailx
CAP=$(df -PH --no-sync "$i" | awk 'NR>1'| awk {'print $5'} | sed 's/.$//')
if [ $CAP -ge $THRESHOLD ]
then
(echo
echo "---------- CAPACITY TEST FAILED ---------- "
echo -n " SYSTEM NAME: " ; uname -n
echo -n " USER DETAIL: " ; whoami
echo " TEST AREA: $i "
echo " USED SPACE: $CAP% "
echo " THRESHOLD: $THRESHOLD% "
echo " !!!!!! THRESHOLD EXCEEDED !!!!!! ") | tee >(mailx -s "$SUBJECT" -r "$FROM" -S replyto="$REPLYTO" "$TOLIST")
echo
else
echo
echo "++++++++++ CAPACITY TEST PASSED ++++++++++ "
echo " TEST AREA: $i "
echo " USED SPACE: $CAP% "
echo " THRESHOLD: $THRESHOLD% "
echo " !!! SUCCESS SUCCESS SUCCESS SUCCESS !!! "
echo
fi
done
exit 0
This works perfectly! But I haven’t a clue how to restructure this to include a –nomail option. It seems ‘case’ might be prudent but I am lost here.
Any thoughts?
Thanks so much!
donesomewhere in the script (for the firstforloop) - is your code one single script or are you showing two different alternative versions of the for loop?