exiting from a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting exiting from a loop
# 8  
Old 11-26-2008
Quote:
Originally Posted by chatwizrd
Why do you need a exit, break or continue have you tried just without all those?
The reason I need to quit from the loop is because each function is required only upon the success of the previous function, so in this case, I dont want to waste the time and resources attempting to run function2 if function1 has failed, (there are many of these related functions which are all reliant on the success of the previous one but my example shows just two)

If function1 has failed (i.e. a ping check fails in the example given above) I do not want it to move on to function2 (which checks the contents of a file) but to instead quit out of that loop and move onto the next loop item which will start at function1 again

The idea being like a series of sequential tests with only the loop items that pass every function test getting to the very last function

I can achieve this successfully via a series of nested if's upon more nested if's or indeed if I place the if statements within the body of the "for" loop (as stated above)...but i dont really want to do that as I would prefer to get this all working with functions

hope that makes it clearer
# 9  
Old 11-26-2008
I just updated ... guess thats what I get for not reading the original post :P
# 10  
Old 11-26-2008
Quote:
Originally Posted by chatwizrd
I dont know what os you use but the syntax for ping doesnt work on any os that I use.

I had to change it to:

ping $1 -q -c 2
im using solaris and the ping command works fine, im not so worried about the contents of the functions to be honest, its more a question on how the "continue" command works and the difference between it when run inside a "for" loop or when it is called via a function
# 11  
Old 11-26-2008
ah , ok so i need to define my functions within the loop ?
# 12  
Old 11-26-2008
ok, ive just fully tested this, if I define my functions within my "for" loop (as chatwizrd suggested) and call them (again, within the "for" loop) it still doesn't work at all !!

the only way I seem to be able to get this working is if I ditch the whole idea of functions and just put "if" statements within the "for" loop with the "continue" command used to quit the loop when a particular condition fails

It would be a real shame if this is the only way to get this working as I was really hoping for a nice tidy script possibly with my functions defined in an external functions file that I source into the script.

Any additional suggestions on how to get this working with functions would be greatly appreciated

Cheers
# 13  
Old 11-26-2008
doing 'man ksh' yields the following:
Code:
     * break [ n ]
           Exit from the enclosed for, while,  until,  or  select
           loop,  if any.  If n is specified then break n levels.
           If n is greater than the number  of  enclosing  loops,
           the outermost enclosing loop shall be exited.

     * continue [ n ]
           Resume the next iteration of the enclosed for,  while,
           until,  or  select loop. If n is specified then resume
           at the n-th enclosed loop. If n is  greater  than  the
           number  of  enclosing  loops,  the outermost enclosing
           loop shall be used.

     * return [ n ]
           Causes a shell function or '.' script to return to the
           invoking script with the return status specified by n.
           The value will be the least significant 8 bits of  the
           specified  status.  If  n  is  omitted then the return
           status is that of the last command executed. If return
           is  invoked  while  not in a function or a '.' script,
           then it is the same as an exit.

     * exit [ n ]
           Causes the calling shell or shell script to exit  with
           the  exit status specified by n. The value will be the
           least significant 8 bits of the specified status. If n
           is  omitted  then  the exit status is that of the last
           command executed. When exit occurs  when  executing  a
           trap, the last command refers to the command that exe-
           cuted before the trap was invoked. An  EOF  will  also
           cause  the  shell to exit except for a shell which has
           the ignoreeof option (See set below) turned on.

Pick your poison - for return from shell functions use 'return'.
# 14  
Old 11-26-2008
Thanks, I did actually see that before, however when I replace my "continue" with a "return" it completely ignores it ... I seemed to be getting a little more joy with "continue"

Is there a special way I should be using "return" ? am i doing this wrong. I thought "return" was used to pass function results back to the script, not to quit you out of an iteration of the loop ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop exiting

Hi , I am processing some files using below shell script the problem for loop exit after processing some files even though it exist.After modifying file.txt and rerunning the script and its running .Any Advise for i in `cat /xx/file.txt |tr -s "," '\n' ` ; do echo $i... (3 Replies)
Discussion started by: mohan705
3 Replies

2. Shell Programming and Scripting

While Loop Exiting

We are trying to design a flow so that an ETL job shouldn't start until the previous job completes. The script we have written is while ; do sleep 2; done The loop however exits even when the process is actually running. Why could this be happening? (12 Replies)
Discussion started by: jerome_rajan
12 Replies

3. Solaris

Exiting signal 6

Hello all, I have a problem when installing Solaris 10 on Enterprise 450. I booted from dvd, then the installation was started. The error appeared after determining the installation method, F2-Standard, F?-Flash...... The error was Exiting signal 6. Please, need help. Thank you (4 Replies)
Discussion started by: Hardono
4 Replies

4. Shell Programming and Scripting

Problem exiting a WHILE loop in ksh

Hi I am having a problem exiting a WHILE loop. I am on a Sun server using ksh. I am running a Veritas Cluster Software (High Availablity) command to obtain a group status and grepping the command output for status "G" which means that the filesystem is frozen and therefore not available to... (3 Replies)
Discussion started by: bigbuk
3 Replies

5. Shell Programming and Scripting

Loop Forever Script Strangely Exiting

Hi, I have a really simple script which I want to run forever, inside the loop it runs a C application which if it exits should restart. #!/bin/sh while true do ./SCF scf.conf >> scf.log sleep 2 done For some reason the SCF C application coredumps and the script is exiting.... (3 Replies)
Discussion started by: marvinwright
3 Replies

6. Shell Programming and Scripting

Problem in exiting a loop

Hi my code looks like: if test $STEP -le 10 then . . ls -1d AM*-OUT|while read MYDIR do cd $MYDIR ls |tail -n1| while read MYFILE do . . if test -s $MYFILE then sqlldr .... rc=$? if test $rc -ne 0 (3 Replies)
Discussion started by: anijan
3 Replies

7. UNIX for Advanced & Expert Users

"while read ..." loop exiting after reading only one record

Greeting, The following script completes after reading only one record from the input file that contains many records. I commented out the "ssh" and get what I expect, an echo of all the records in the input.txt file. Is ssh killing the file handle? On the box "uname -a" gives "SunOS... (2 Replies)
Discussion started by: twk
2 Replies

8. Shell Programming and Scripting

Else Loop Exiting Early

All, I'm having a problem w/this function. Specifically, I want to call another function (get_stats) when the process in the else completes (the initial if and the elsif seem to work fine). But what's happening is the get_stats function call is running after the else runs only once, NOT when it... (8 Replies)
Discussion started by: GregWold
8 Replies

9. Shell Programming and Scripting

Bash: Exiting while true loop when terminal is not the focus window

I am running an Ubuntu Gutsy laptop with Advanced Compiz fusion options enabled. I am using xdotool to simulate keyboard input in order to rotate through multiple desktops. I am looking for a way to kill a while true loop when the Enter key (or Control+C if it is easier) is pushed when the... (2 Replies)
Discussion started by: acclaypool
2 Replies

10. Programming

exiting in c

how can i exit in a c program i have tried system ("exit"); but this doesnt seem to work just wondered if you could help. (3 Replies)
Discussion started by: ruffenator
3 Replies
Login or Register to Ask a Question