Execute only part of the code if the statement is TRUE

Issue

if [[ $current_version != $new_version ]]; #new version is available
then
   mv_date=`date +'%H'`
   if [[ $mv_date -eq 9 ]];
    then
    aws sns publish --topic-arn "arn:aws:sns:xxxxxxxxxxxx:maintenance-test" --message "New Engine Version : $new_version is available for $database database." >>  $engineversion_folder/m3.txt
                    
        elif [[ -f $engineversion_folder/m3.txt ]] && [[ -s $engineversion_folder/m3.txt ]];
    then 
    echo "Notification already sent!"               
    fi
else
    rm -rf  $engineversion_folder/m3.txt
fi

Hi, this is my sample code. This script is set to run every hour, but I only want to be notified only once if it meets the condition. Given that the statement is still true, I only want to receive a notification once at 9 a.m., as specified in the code. When the code runs again, I should not receive another email informing me that there is a new version.
What should I change in my code to make this one work?

If current version differs from new version, it will notify me via email that a new version is available. I also save the output to another file.

Another condition I added is if the file already exists and is not empty, which means that notification has already been sent to me via email and I should not be able to receive another email even if the statement remains true.

When the condition is FALSE (current version = new version), the created text file is simply deleted.

Solution

Looks like you need to hoist the if test on the $engineversion_folder/m3.txt file existence earlier, perhaps something like this:

   if [[ $mv_date -eq 9 ]];
    then
    if [[ -f $engineversion_folder/m3.txt ]] && [[ -s $engineversion_folder/m3.txt ]];
    then 
      echo "Notification already sent!"  
    else
      aws sns publish --topic-arn "arn:aws:sns:xxxxxxxxxxxx:maintenance-test" --message "New Engine Version : $new_version is available for $database database." >>  $engineversion_folder/m3.txt       
    fi

Answered By – Dan Bonachea

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published