How to send array values by mail

I had a case where I had a pool of servers. And I wanted email notifications if any server or some of them meet the condition. The condition was if my rsync exceeds X number of files, send me a mail. The problem came in the moment when only 1 server met the condition but I received mails for all of them. It was annoying, spamming and WRONG.

The example I am going to show you does NOT include the rsync part because it is simply useless but you will see simulation with a predefined value.

 

#!/bin/bash
set -x

pool=”s1 s2 s3″
emptyvar=””
sendmail=false

number=”6″

for HOST in $pool; do
emptyvar=”$emptyvar $HOST:”
echo “111111111111”

if [[ $number -eq 5 || $1 == true ]];then
echo “No mail”
else
sendmail=true
emptyvar=”$emptyvar $number”
fi
done

echo “mid”

if $sendmail;then
echo “true”
echo “Test $emptyvar $HOST” | mutt -s “Test” somemail@blah.com
exit 1
else
echo “false”
echo “continue to actual”
fi

echo “END”