Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If else question

Status
Not open for further replies.

HHG

Technical User
Nov 8, 2003
68
GB
Hi All

Happy new year to you all. Hope your Xmas was good too.

I have a question, in my existing script I carry out the following:-

if test ...
then
do whatever I want
fi
else
send an email to notify users of the problem
end

This script runs in cron every 10 minutes. When the script runs it carries out a check in a directory to see if a file exists if it does then the script continues. If the file doesn't exists it then sends an email out and exists the script. I need to make my script only send an email out once so that the next time the script runs I need to check if an email has already been sent out so don't send another email out. I am ending up with hundreds of notification emails.

Basically in words within the else statement above I need a further check not sure how to best do this. Anyone have any clues would be most grateful.

Many thanks in advance.
 
Hmmm ...

You could try something like that:

if file is found ...
then
do something
echo "0" > /tmp/check.out
else
check=$(cat /tmp/check.out)
if [ $check = 0 ]
then
send email
echo "1" > /tmp/check.out
fi
fi

So that after the first time the script finds out the file doesn't exist the script writes a "1" into the checkfile after sending an email and as long as the file does not exist it won't send another email because the "0" is only being written the next time the script finds out that the file DOES exist.

Just my first thought ...
However I didn't test it out ...

Regards
Thomas
 
A variant on Thomas's idea is to use the existance of the flag file - i.e.
Code:
#!/bin/ksh
REAL_FILE=/path/to/real/file
FLAG_FILE=/path/to/flag/file
if [ -f $REAL_FILE ]
then
  do something
  # Delete FLAG_FILE if found
  [ -f $FLAG_FILE ] && rm -f $FLAG_FILE
else
  # if FLAG_FILE found no further action
  [ -f $FLAG_FILE ]] && exit
  # Send message
  echo $REAL_FILE not found | mail -s "No file message" me@myhost
  # Create FLAG_FILE to prevent further messages
  > $FLAG_FILE
fi

Ceci n'est pas une signature
Columb Healy
 
Hi both thanks for this. I think I need to explain the bigger picture.

The first thing this script does is a check for a READY file held in a directory. If its found then

the script removes the READY script then does something, part of that something is sends files across ftp to another server. Once the files has completed the transfer part of the script get the READY file and places it back into the original directory where the script carries out the original test when it first runs.

If the script doesn't find the READY file next time it runs then within the else statement an email get sent once. So I have 2 problems.

Problem 1 - to stop emails been sent more than once or twice
Problem 2 - Is within the else statement I need to connect to the ftp line and get the READY file again, so that the next time this script runs again I need to check if an email has already been sent and if the ready file exists then to send another email out to inform users that all is backup and running again. Not really sure how I am going to do this so need your help please.

e.g.

if READY file exist and email flag is set then send ok email out
if Ready file exist and no emails have been sent out then continue with the script as normal.
if doesn't exist then check if email already sent out if not send email if it has then don't send another email.

 
So each time the script runs the possible states are
[ol]
[li]All OK and as normal - Ready flag exists - do standard transfer[/li]
[li]Ready flag missing, no email sent - send error condition email[/li]
[li]Ready flag missing, email already sent - NFA[/li]
[li]Ready flag present AFTER previous error condition - send 'back in business email[/li]
[/ol]
In pseudo code assuning we're using a flag file to denote the error condition
Code:
if Ready Flag and not error flag
  send files
if not Ready flag and not error flag
  send error email 
  set error flag
if Ready flag and error flag
  send files
  send back in business e-mail
  delete error flag
if not Ready flag and error flag
  NFA
Rather than trying to create some complex if...else statement we now have a 'case' statement. There's another thread thread822-1440389 which deals with this. Have a look and see how you get on.

On the internet no one knows you're a dog

Columb Healy
 
We have a similar script. We just make a copy of the file
once the email is sent (for example, ready.copy).
Each time the script executes it performs a cmp of the
current file to the copy, if they are the same, it does not
send an email. If it is different, then it sends the email
and copies the file again. You can create an empty ready.copy with the touch command to force the script to execute for the very first time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top