I am working on a script.
- The script runs on multiple servers.
- It sends an email to the address mentioned in the TO field.
- There can be one of the 2 recipients of the mail ([email protected] or [email protected]), depending on the server on which the script is executing.
- There's a list of 4 pre-defined servers. If the script is executing on one of these servers, it sends the email to [email protected]. Else, it sends the email to [email protected].
I thought this is correct, but I missed a logic that on every server both if and else condition will be checked.
#!/bin/bash
set -x
SERVER_NAME=$(hostname -s)
FILE_TEMP=/tmp/new.log
echo -e "This is a test from SERVER_NAME" >> $FILE_TEMP
subject="sending file from $SERVER_NAME"
Servers=(flipunix1 flipunix2 flipunix3 flipunix7)
for i in ${Servers[*]}
do
if [ "$SERVER_NAME" == "$i" ];
then
to="[email protected]"
echo -e "server name picked is $SERVER_NAME and i value is $i "
break
else
to="[email protected]"
echo -e "server name picked is $SERVER_NAME and i value is $i "
break
fi
done
also_to="[email protected]"
mail -s "$subject" "$to" "$also_to" < $FILE_TEMP