I have to construct shell script to check the SFTP status,
Define a global variable (say sftpStatus). Set it to default value "success" when you define it first time outside the script.
check the current SFTP status (say currentStatus - local variable within the script)
if (sftpStatus is success and currentStatus is failed)
send an alert to support team
set sftpStatus to failed
else
if (sftpStatus is failed and currentStatus is success)
send service restoration mail
set sftpStatus to success
No alert is needed if both these variables are success or failed
Ok. Looks like you want to retain the value of these 2 variables each time you run your script. I suggest storing these variable values in files. Here is a template of how you can achieve this, you can modify as per your need:-
sftpSTATUS=`cat sftpstatus.dat 2> /dev/null`
if [ -z "$sftpSTATUS" ]
then
echo "success" > sftpstatus.dat # Setting default status for sftpSTATUS
fi
echo "#Status of SFTP service (success/failed)" > currentstatus.dat
currentSTATUS=`cat currentstatus.dat 2> /dev/null`
if [ "$sftpSTATUS" = "success" ] && [ "$currentSTATUS" = "failed" ]
then
# send an alert to support team
echo "failed" > sftpstatus.dat
elif [ "$sftpSTATUS" = "failed" ] && [ "$currentSTATUS" = "success" ]
then
# send service restoration mail
echo "success" > sftpstatus.dat
fi
Is it possible to do the same script without calling the file name outside the script with golbal variable if yes, can you please provide me script with golbal variable
When the script execution terminate, the value stored in the variable will be lost. This is the reason why we have to store it in a file or DB. AFAIU you want this variable value available during each execution which is why I suggested this approach.