Home > Back-end >  If statement that works too good
If statement that works too good

Time:10-17

I'm working on this script and it's doing it's job. But it's too good... I want it to send me the IP of the server to my email (I have a dynamic IP at the place where the server is) and log it to a file (AAA) as well. The problem is that if the IP is the same as it was before it will echo "same" as it should, but still sends the email while it shouldn't. Am I missing something super obvious here?

The script is work in progress.

#! /bin/bash

catvar=$(cat AAA |tail -1 |awk '{ print $2 }')
date=$(date  "%d-%m-%y")
add1=$(wget -qO- icanhazip.com)
sendIP=$(sendemail -f ... the whole syntax with sensitive data) 

#echo $sendIP
#echo $date
#echo $add1
#echo $catvar

#######It's sending me email in both cases. 
if [ "$catvar" != "$add1" ];
then echo $sendIP; echo "$date $add1" >> AAA; 
else echo same; 
fi

CodePudding user response:

You are doing the sending before checking the condition, here, in this line:

sendIP=$(sendemail -f ... the whole syntax with sensitive data)

If you want want to do the sending only when the IPs are different then move that line inside if block:

if [ "$catvar" != "$add1" ]; then
    sendIP=$(sendemail -f ... the whole syntax with sensitive data)
    echo $sendIP
    echo "$date $add1" >> AAA
else
    echo same
fi
  • Related