LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-27-2020, 11:24 PM   #1
orangepeel190
Member
 
Registered: Aug 2016
Posts: 69

Rep: Reputation: Disabled
If Loop - Assistance Exiting


Hi All,

I am trying to get a loop bash script up and running .... I am trying to get a search loop up and running ... once its finds the latest file, it commands one section of the script, if not, it sleeps for a while and then searches again ...

Once the loops are finished - it reports such and (may) action other tasks.

Code:
echo "Starting loop until File ($file) found ..."
for i in {1..10}; do
if wget -N $url/$file | grep 304; then
	echo "File $file found ..."
       if [ $get_file == "yes" ]; then
		echo "Time to Download the Latest File -> $file"
       fi
#
       if [ $home_backup == "yes" ]; then
     		echo "Making Home Backup on NAS Server [ XXX Folder ]"
		home_backup
       fi
       echo "Breaking Loop .... Job Complete"
	break
else
	echo "($file) NOT modified on server. Omitting download"
       echo "Sleeping for $sleep until trying again...zzZZzz"
   	sleep $sleep
fi
# Fall back if nothing found or task not completed...
# Should not really end up here...
#echo " Loops Completed - Nothing Found or Task not Completed - Try again"

done
I am trying to figure out who to exit the script so that the "Fallback" section of the script only runs when the loops have finished and either of the other "IF" have not been successful.

I am trying to implement an ELIF but having a problem to get it to work as such based on the initial IF task. I am thinking the problem is in the last part of the script, and placement of the IF,THEN,ELSE etc etc...

Is there a way to exit out of the entire script - say after the "BREAK" if reached?

Anyone know a way around this to tidy it up and not run specific sections of the script unless triggered....

Thanks a million!
 
Old 07-27-2020, 11:50 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
If you want to run the fallback section when the loop has finished, you should place it outside of the loop.

I only see a single loop there, by the way. Why do you talk about loops (plural)?

EDIT: Another question: Why do you loop ten times? The first line indicates that you want to loop until you find a file. A while loop rather than a for loop would be the best approach here.

EDIT2: The line "Should not really end up here" is incorrect. You will definitely end up there in case you go through the else branch of the outer if.

Last edited by berndbausch; 07-27-2020 at 11:56 PM.
 
Old 07-28-2020, 12:00 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by orangepeel190 View Post
Is there a way to exit out of the entire script - say after the "BREAK" if reached?
Sure. Use exit.

To be honest, after looking at your description and the script, I am not quite sure what doesn't work, and what your question is.
 
Old 07-28-2020, 12:41 AM   #4
orangepeel190
Member
 
Registered: Aug 2016
Posts: 69

Original Poster
Rep: Reputation: Disabled
The plan is to schedule via cron. Looping every 6-12 hours searching for when the file is updated on the website. The aim is to grab the file (will be commanded inside the script).

Having a limit on the loops wk ensure that it is not running all the time.
 
Old 07-28-2020, 12:44 AM   #5
orangepeel190
Member
 
Registered: Aug 2016
Posts: 69

Original Poster
Rep: Reputation: Disabled
I have tried “exit” after the Break (inside that IF statement - before the ELSE) but it didn’t work. Not sure why....

Amended code:-
Code:
        fi
       echo "Breaking Loop .... Job Complete"
	break
        exit 0
else

The comment about “Should not end up here” is that the file should be present on the website and the script “ends up here” the file was not uploaded in time

Thoughts?

Last edited by orangepeel190; 07-28-2020 at 01:02 AM.
 
Old 07-28-2020, 01:29 AM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by orangepeel190 View Post
I have tried “exit” after the Break (inside that IF statement - before the ELSE) but it didn’t work.
It doesn't work because the break command jumps out of the loop. As a consequence, exit is not executed. You need to replace break with exit.

Again, to execute some code when the loop is complete, that code should be outside of the loop.

I am still not sure what you are asking. To paraphrase your program:

You perform a wget. If the output of wget contains the string "304", you echo a few things, conditionally run a backup, then jump out of the loop.
If the output of wget does not contain "304", you sleep a few seconds.

The above is done up to ten times.


If that is your aim, I think your program works. If it doesn't work, tell us exactly what doesn't work.

Last edited by berndbausch; 07-28-2020 at 01:30 AM.
 
Old 07-28-2020, 05:14 AM   #7
orangepeel190
Member
 
Registered: Aug 2016
Posts: 69

Original Poster
Rep: Reputation: Disabled
Thats it. I had the exit in the wrong place. Ive replaced Break with Exit and it is performing as I wish.

Basically I am writing a script to start on a certain day - run for numerous loops over the coming day (sleeping for 8-10hours at a time) before checking again that the latest file has been uploaded to the website. Once the latest file has been uploaded (should not return the 304 from the header) than it triggers another section of the code (download, send to remote server/backup etc) and then exit out.

The loop duration is shorter than the next cron start time - so no overlapping. Cron might start on a Thursday and run for x days in the loop (say 10 loops) and then end (either when found the latest file or run out of loops (as the file has not been uploaded in time).

Appreciate your help. I will see how it goes this Thursday onwards and report back if it worked as planned or requires some tweaking!

Appreciate your guidance - such a simply think was throwing out the script.
 
Old 07-28-2020, 06:40 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Keep in mind that executing wget as root might be a security risk (esp. in combination with unquoted shell variables).
Consider using
Code:
sudo -u orangepeel wget ...
instead.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script running ssh commands (via loop) exiting after first iteration kirecali Linux - Server 3 10-27-2010 05:41 AM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
Exiting BASH function without exiting sourced environment SwingingSimian Programming 10 08-12-2009 12:59 AM
infinite loop not exiting when killed kinkle Linux - General 1 04-11-2009 07:55 PM
Function suggestions... and while loop isn't exiting in dialog manwithaplan Programming 1 03-13-2009 08:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration