Breaking out of loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Breaking out of loop
# 1  
Old 12-10-2010
Breaking out of loop

I have a main script with while loop having for loop inside. Again in for loop based on if condition few functions will be called. So when a function is called for certain condition it should come out from the main for loop and should continue with while loop.
Let me explain with example here:
I am having a ksh as below
Code:
###Functions#####
execute_sqlldr()
{
sqlldr user/pass control=$ctl log=$log data=$file discard=$dis
RC=$?
  if [$RC -eq 0] then 
      echo "loaded successfully"
  elif [$RC -eq 1 -o $RC -eq 3]; then
      echo "sqlldr problem"
  elif [$RC -eq 2]; then
      ??
  fi
return 0
}
 
execute_sp()
{
-----
}
 
###Main Program####
while [1]
do
  for file in `ls /home/usr/*.txt`
  do
      if grep 'Oracle Available'; then
          execute_sqlldr $file
      else 
          echo "database is not available"
      fi
 
      execute_sp $file
     done
done

Here in the main program when execute_sqlldr $file is called and when $RC -eq 2 then my program instead of coming back to for loop in main program and calling the next function execute_sp $file it should come wholly out of for loop and should start from while loop. So with what should i replace ?? with in the function execute_sqlldr().
I hope i am clear here. Any help is greatly appreciated. Thanks Smilie
# 2  
Old 12-10-2010
Return some unique no from execute_sqlldr (when $RC -eq 2), say return 2
Code:
execute_sqlldr()
{
sqlldr user/pass control=$ctl log=$log data=$file discard=$dis
RC=$?
  if [$RC -eq 0] then 
      echo "loaded successfully"
  elif [$RC -eq 1 -o $RC -eq 3]; then
      echo "sqlldr problem"
  elif [$RC -eq 2]; then
      return 2
  fi
return 0
}

Then
Code:
while [1]
do
  for file in `ls /home/usr/*.txt`
  do
      if grep 'Oracle Available'; then
          execute_sqlldr $file
          if [ $? -eq 2 ]; then
             break 1
          fi
      else 
          echo "database is not available"
      fi
 
      execute_sp $file
     done
done

In break n, n specifies the nth enclosing loop to exit from.
# 3  
Old 12-10-2010
dont we have something like break or continue which we can use instead of returnning a value?
# 4  
Old 12-11-2010
You should space around the brackets:
Code:
[ $RC -eq 0 ]

Code:
[ $RC -eq 1 -o $RC -eq 3 ]

etcetera..
Like Anurag says, you cannot break out of the for loop from within the function. You can only pass a return code or value which you can test in the main loop and the you can use break. break 1 is the default, so break and break 1 are the same.

Last edited by Scrutinizer; 12-11-2010 at 04:49 AM..
# 5  
Old 12-11-2010
Quote:
Originally Posted by Scrutinizer
You should space around the brackets:

Not 'should', 'must'!
Quote:
Code:
[ $RC -eq 0 ]

Code:
[ $RC -eq 1 -o $RC -eq 3 ]

etcetera..

Use of -o is deprecated. Use:
Code:
[ $RC -eq 1 ] || [ $RC -eq 3 ]

Quote:
Like Anurag says, you cannot break out of the for loop from with the function.

You can in bash and ksh93.
This User Gave Thanks to cfajohnson For This Post:
# 6  
Old 12-11-2010
Quote:
Originally Posted by cfajohnson
Not 'should', 'must'!
I was trying to be polite Smilie
[..]
Quote:
You can in bash and ksh93.
I stand corrected. Also in dash it breaks the calling loop... ThanksSmilie

---------- Post updated at 11:29 ---------- Previous update was at 09:55 ----------

So to sum up, you could use break inside a function which would break out of a calling loop. I would not use that however as I think it is confusing. I would generate a return code and check for that in the calling part.
# 7  
Old 12-13-2010
Thank you friends it worked
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop breaking when using "ssh" command inside

Hi , I am trying to read a list of hosts from a config file and trying to get file list from that host. For this I have used one while loop. cat "$ARCHIVE_CFG_FILE" | sed '/^$/d' | sed '/^#/d' | while read ARCHIVE_CFG do SOURCE_SERVER_NAME=`echo "$ARCHIVE_CFG" | awk -F '|' '{ print... (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

2. Shell Programming and Scripting

Breaking a pipe

Here's my code - for File in "${Files}"; do uuencode "${File}" "$(basename ${File} 2>&-)" 2>&-; done | mailx -s "subject" "a@b.c" Now I want to know if there a way to *not* send an email if the "Files" variables turns out to be blank. Any suggestions? Also, just to clarify, I... (4 Replies)
Discussion started by: nexional
4 Replies

3. Shell Programming and Scripting

Ssh to remote server loop is breaking

hi All, cat login.list server1 userid1 server2 userid2 server3 userid3 ---------------------------------------- #SSHSCRIPT.ksh FILE=login.list while read vah vah_id do ssh $vah -l $vah_id "pwd" done < "$FILE" ----------------------------------------- When i... (2 Replies)
Discussion started by: raghur77
2 Replies

4. Shell Programming and Scripting

breaking for loop

Dear Friends, Here I need your guidance once again. I have for loop which check all files in a folder for a particular string. If the string is found in a file it returns value other than 0 else returns 0 value in variable t2. At times the string which we are looking for is in first file... (1 Reply)
Discussion started by: anushree.a
1 Replies

5. Shell Programming and Scripting

Breaking up a file

Hi, I have a file that looks like this - lets call it fileA >hhm2 IIIIIIIIILLLLLLLMMMMMMMMMNNNNNNNNNNGGGGGGHHHHHHHH >hhm4 OOOOOKKKKKKKKMMMMMHHHHHLLLLLLLLWWWWWWWWWWW >hhm9 OOOOOOOIIIIIIIIIKKKKKKKKKMMMMMHHHHHHHHHHHLLLLLLLLLL So the file is pretty straight forward. The name is indicated... (2 Replies)
Discussion started by: phil_heath
2 Replies

6. Shell Programming and Scripting

Breaking if-else loop and variety of comparisions

Hello Friends, Im trying to write a script to invoke nagios. In order to do this I grep some words that comes from output of some backup scripts. When there is "End-of-tape detected" in directed output logs it should give alarm. First I would like to know if there is any better way to write... (5 Replies)
Discussion started by: EAGL€
5 Replies

7. Shell Programming and Scripting

Breaking Loop by using another script

Hi friends, I have 2 scripts. 1) Master_Script.sh and 2) Sub_script.sh We run Master_script.sh manually where as sub_script.sh keeps generating output in every 2 minutes (through crontab). The output generated by sub_script.sh can be 0 or 1. As I told you, sub-script.sh keeps generating o/p... (7 Replies)
Discussion started by: anushree.a
7 Replies

8. Linux

breaking out of while loop

Hi , I am running this script ( pasting only error code ) to generate some ddl definition for tables . But what I want is to break out of the db2look part when the base_table is not like DIM_$TN or FACT_$TN . After this it should come back to while loop to read the next TN . I read the other... (3 Replies)
Discussion started by: capri_drm
3 Replies

9. Shell Programming and Scripting

rsh breaking me out of loop

Hey all I have two scripts, one script containing the guts of my code. The other simply loops through a list, calling the other script on each iteration. Problem is when I add the line `/usr/bin/rsh -l root $HOSTNAME ""` to my main script, the loop never seems to exectute any more... (1 Reply)
Discussion started by: mark007
1 Replies

10. HP-UX

Breaking Mirror

Can some one point this UNIX newbie to a web site or directions on the steps needed to break a mirror in HP-UNIX to change a bad hard drive. (4 Replies)
Discussion started by: egress1
4 Replies
Login or Register to Ask a Question