exiting from a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting exiting from a loop
# 1  
Old 11-25-2008
exiting from a loop

I wonder if someone could help me here. I am trying to find a way of exiting from a loop but not exiting me from the script for example


Code:
#!/bin/ksh

#  *************   FUNCTIONS    ******************

function1() {      #ping test

   ping $1 2 > /dev/null
      if [ $? != 0 ] ; then
          echo "Couldn't ping $1, skipping"
          exit
      else
          echo "ping to $1 successful, continuing to function2"
      fi
}

function2() {     # now we can ping ...check the location

location=`ssh $1 'cat /etc/location'`

   if [ $location = "London" ] ; then
        loc=ldn
        echo "Location is London, passing on to function3"
   else
        echo "location is NOT London, Skipping"
        exit
    fi
}


#### main script body ####

for n in list 
do
  function1 $n
  function2 $n
  etc
  etc
done


hopefully youll get the general idea, i have a load of functions to perform for each item in my for loop, if they dont "pass" the first function, id like to not have to run through every subsequent function in the loop, id like it to quit there and move onto the next item in the for loop

I tried putting an "exit" and an "exit 0" into the fail criteria of the first function, but that quits me out of the whole script and not just that loop item,

Is there any way of doing this ?

Last edited by hcclnoodles; 11-26-2008 at 08:53 AM..
# 2  
Old 11-25-2008
Have you tried
Code:
exit 1

or any other non-zero value?
# 3  
Old 11-25-2008
Replace exit by break
# 4  
Old 11-26-2008
Quote:
Originally Posted by glen.barber
Have you tried
Code:
exit 1

or any other non-zero value?
Thanks for the reply but whatever value of exit i give, it throws me out of the script completely


Quote:
Originally Posted by danmero
Replace exit by break
Thanks danmero, ive just tried this and it doesn't work, I'm not sure why because looking at examples on the web it seems as though it should do... I'm going to lay out my example code a little better here (because my functions are actually defined at the top of the script)

It seems the break is being ignored and moving on to the next function regardless

Code:
#!/bin/ksh

#  *************   FUNCTIONS    ******************

function1() {      #ping test

   ping $1 2 > /dev/null
      if [ $? != 0 ] ; then
          echo "Couldn't ping $1, skipping"
          break
      else
          echo "ping to $1 successful, continuing to function2"
      fi
}

function2() {     # now we can ping ...check the location

location=`ssh $1 'cat /etc/location'`

   if [ $location = "London" ] ; then
        loc=ldn
        echo "Location is London, passing on to function3"
   else
        echo "location is NOT London, Skipping"
        break
    fi
}


#### main script body ####

for n in list 
do
  function1 $n
  function2 $n
  etc
  etc
done

The break doesn't seem to be working, it will still go through all the functions sequentially for each loop item, it doesn't hit (for example) the fail condition of function1 and move on to the next loop item skipping the rest of functions for that one, it just echoes the fail message from function1, ignores the break and then moves on to function2
# 5  
Old 11-26-2008
ok, ive done a bit of digging and it seems "continue" is the command im after to drop me out of the loop. However, the strangest thing has come to light....If i put my "continue" statement within my "if" statement within my "for" loop, then everything works as expected by throwing me out when there is a failure on the first "if" statement.... like this

Code:
#!/bin/ksh

for n in list 
do

     # what was function1
   ping $n 2 > /dev/null
      if [ $? != 0 ] ; then
          echo "Couldn't ping $n, skipping"
          continue
      else
          echo "ping to $n successful, continuing to function2"
      fi


     # what was function2
      location=`ssh $1 'cat /etc/location'`
   if [ $location = "London" ] ; then
        loc=ldn
        echo "Location is London, passing on to function3"
   else
        echo "location is NOT London, Skipping"
        continue
    fi

done


Now that is exactly what I want, apart from the fact I have had to put what were externally defined functions, physically within the for loop.

If i define them outside of the for loop and call them in, the continue statement is ignored

so the following code doesnt work

Code:
#!/bin/ksh

#  *************   FUNCTIONS    ******************

function1() {      #ping test

   ping $1 2 > /dev/null
      if [ $? != 0 ] ; then
          echo "Couldn't ping $1, skipping"
          continue
      else
          echo "ping to $1 successful, continuing to function2"
      fi
}

function2() {     # now we can ping ...check the location

location=`ssh $1 'cat /etc/location'`

   if [ $location = "London" ] ; then
        loc=ldn
        echo "Location is London, passing on to function3"
   else
        echo "location is NOT London, Skipping"
        continue
    fi
}


#### main script body ####

for n in list 
do
  function1 $n
  function2 $n
 
done


I just dont understand why it works if i embed the code, but it doesnt if I define it as a function and call it in - what is the difference, arent functions treated as embedded code when they are called in ?????

I really would prefer to keep this function based as the code is a lot tidier

Is anybody able to explain to me why this happens....im stumped Smilie
# 6  
Old 11-26-2008
You need to do your break down on the for loop at the bottom not in the functions themselves.

Last edited by chatwizrd; 11-26-2008 at 05:58 PM.. Reason: fixed things
# 7  
Old 11-26-2008
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
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