Need info on looping

My script will do:

while true
do
	if [[ -s file.txt ]]; then
		### here im mailing content of file.txt
	fi
	
	if [[ -e End_of_day.log ]]; then
		exit 0
	fi
	
	sleep 30m
done

If i add exit 1 after ### mail, script is getting stopped.
Can you please suggest, how to implement above requirement. Mine is Linux machine.

use break instead of exit 0

Hi,

If what you're needing to do is make sure the script does an exit 1 if it's had to send a mail, but doesn't do it until the presence of End_of_day.log , then you could amend your script like this, maybe ?

while true
do
        if [[ -s file.txt ]]; then
                ### here im mailing content of file.txt
                abend=1
        fi
        
        if [[ -e End_of_day.log ]]; then

                if [ "$abend" == "1" ]
                then
                        exit 1
                else
                        exit 0
                fi
        fi
        
        sleep 30m
done

So basically, set a variable (in this case I've called it abend ) after you send the mail, and later on when it's time to exit the script check for the presence of that variable and set your exit value depending on whether or not that variable is set.

Hope this helps - if not, let us know what about this solution doesn't work and I'll have another crack at it.

An alternative to remove an if ... then .... else ... fi section:-

while true
do
        if [[ -s file.txt ]]; then
                ### here im mailing content of file.txt
                abend=1
        fi
        
        if [[ -e End_of_day.log ]]; then
                exit $abend
        fi
        
        sleep 30m
done
1 Like

How about

until [ -e End_of_day.log ]
  do [ -s file.txt ]  && mail your stuff
     sleep 30m
  done

@drysdalk, @rbatte1
Thanks for reply. I tried the code suggested, but couldn't identify that script abended after mailing.
Can you please clarfy how the script abend after mailing (but continue the loop to check for the file.txt till End_of_day.log present )

Hi,

Hmm, that's odd. In the code provided, it should (in theory at least) be impossible for the script not to set an exit value of 1 on termination if it has had to send an e-mail.

You say you "couldn't identify that script abended" - how are you attempting to identify this ? No error message or warning or anything will be printed, all that will happen (in both the code I provided, and the variant suggested by rbatte1) is that the script will set its exit value to 1 if it had to send a mail at any point during its run, and will exit with 0 as normal if it didn't. You'd have to be checking its exit value to know which of those two things happened.

One last question springs to mind, just in case: what shell are you using ? I'm assuming it is Bash, since you've said you're on Linux.

Hi @drysdalk , its -bash

Hi,

OK, great. That's fine then, the supplied code should definitely work. So if you can answer the other questions (about how you're attempting to identify the abnormal termination of the script, and why you believe you can't detect this) we can take things from there.

im calling the script from control-m scheduler
abnormal termination of the script will be detected with exit 1 or 2 or 5. Successful is exit 0
with the provided changes, it is mailing till End_of_day.log present but job didn't abend.

Hi,

Hmm, I'm sorry, I don't know what else to suggest at this point. You could maybe try adding some debugging code to capture the output if you can do that, to see when the abend variable is being set, and what exit command your script is attempting to do.

But if you have made the changes I suggested (or some variation thereof), there can't be any way that the script will fail to do abend=1 after sending the mail (unless the very act of sending the mail causes some kind of uncontrollable crash, I suppose, but that's unlikely to say the least), and likewise there can't be any way it will fail to do an exit 1 if abend is set at exit time.

If I only knew how you use the verb "abend" - my dictionary tells me "system / program crash" or "abnormal termination".

Why not a function? try

proc2eod ()     { XTCD=1
                  until [ -e End_of_day.log ]
                    do  [ -s file.txt ] && XTCD=0 && echo mail your stuff
                        sleep 3
                    done
                  return $XTCD
                }