Need info on looping

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Need info on looping
# 1  
Old 04-04-2017
Need info on looping

My script will do:
Quote:
In infinite loop,
If file.txt has data, mail the content of it
wait for 30 minutes, repeat above step.
At the same time, keep checking if End_of_day.log is present.
If End_of_day.log present, stop infinite loop.
Code:
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

Quote:
In addition to mailing, Script should abend/fail, but continue the loop till End_of_day.log present
If i add exit 1 after ### mail, script is getting stopped.
Can you please suggest, how to implement above requirement. Mine is Linux machine.
# 2  
Old 04-04-2017
use break instead of exit 0

Last edited by rbatte1; 04-04-2017 at 12:28 PM..
# 3  
Old 04-04-2017
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 ?

Code:
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.
# 4  
Old 04-04-2017
An alternative to remove an if ... then .... else ... fi section:-
Code:
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

This User Gave Thanks to rbatte1 For This Post:
# 5  
Old 04-04-2017
How about
Code:
until [ -e End_of_day.log ]
  do [ -s file.txt ]  && mail your stuff
     sleep 30m
  done

# 6  
Old 04-05-2017
@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 )
# 7  
Old 04-05-2017
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping problem

I need help. I am trying to get this script to send out only one email not multiple emails of the abend. Currently it will send me one then and ther with the first and second one then another email with the first second and third abend and so on. I only want one email sent. ... (2 Replies)
Discussion started by: bbcarosi
2 Replies

2. Shell Programming and Scripting

Looping

Hey guys, so I am trying to do a loop script that will go through each folder (no gui so just each domain has a folder) and grab out the databases being used on that domain. I know I would use mysql -e "show databases where not 'information_schema';" once in each directory to pull the actual... (3 Replies)
Discussion started by: dough
3 Replies

3. Shell Programming and Scripting

Looping advice

Hello guys i am generating files in while loop and i want add value in every file start from 10 to 55 and repeat that so the value never exceed 55 and always start from 10 can you advice how this can be done in while loop? (4 Replies)
Discussion started by: mogabr
4 Replies

4. Shell Programming and Scripting

Looping

Hi evryone i need a help . i have a file xcv.the content is : accelerate i want a script which will run 1000 times in loop and changing the value to accelerate to acceler in 1st loop and in 2nd loop it will be again accelerate and so on . (6 Replies)
Discussion started by: Aditya.Gurgaon
6 Replies

5. Shell Programming and Scripting

Looping

Hi, Now I have written a script which sorts the records in the file and splits them according to some condition. Now, I need to modify the script so that it reads all the files one after the other and does the sorting & splitting. Pls help me in reading all the files in a directory and... (8 Replies)
Discussion started by: Sunitha_edi82
8 Replies

6. Shell Programming and Scripting

help with looping

vesselNames values: xxx yyy zzz vesselPlanned values: xxx zzz zzz zzz OIFS="" OIFS=$IFS IFS="\n" (2 Replies)
Discussion started by: finalight
2 Replies

7. Shell Programming and Scripting

help on looping using if/for or while

Hello, where can I get usefull information on the use of looping with for , if and while with extensive examples. Also use of variables in scripts (1 Reply)
Discussion started by: sam4now
1 Replies

8. Shell Programming and Scripting

for looping

I run into a issue when I try to do sorting of the following with ascending order, one round of for looping seems not working, anyone knows how to use shell or perl? $array = (5,0,3,2,7,9,8) (2 Replies)
Discussion started by: ccp
2 Replies

9. Shell Programming and Scripting

looping

Hi I have around 100 users in sun server and have default home directory in /usr/home/<username> I want to clean their home directory time to time to make free space on root, as users generate many output files during usage of application. My idea is, generate a file with following command... (4 Replies)
Discussion started by: ishir
4 Replies

10. UNIX for Dummies Questions & Answers

Help with looping

Hi, Actually I have a file which consists data . for eg names. Then I want my sql query to read this file and produce the output. Currently I am using this FOR EG : FILENAME is NAMES for i in `cat NAMES` { sqlplus -s $CONNECTID << EOF spool rooh set heading off select... (1 Reply)
Discussion started by: rooh
1 Replies
Login or Register to Ask a Question