Problem in exiting a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in exiting a loop
# 1  
Old 03-23-2010
Problem in exiting a loop

Hi my code looks like:

Code:
 
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
  then
    exit $rc
  fi
 fi 
 done 
 cd ..
done
fi

the exit highlighted exits from the current while loop and again the first while loop continues for next search of directory(highlighted in blue). I want to exit out of the beginning if loop ( exit out of both the while loops). please help. i tried with break instead of exit. tat is also not working.

Last edited by anijan; 03-23-2010 at 08:55 AM.. Reason: use code tags please,ty
# 2  
Old 03-23-2010
Hm what you describe should be the effect of break, not of exit. exit leaves the whole script, not just one loop. What shell are you using?
# 3  
Old 03-23-2010
I think it is working fine now... sorry.. it was a mistake from myside. Is there any way to retain the exit code ( or some value) for the next run of the script.?
# 4  
Old 03-23-2010
If you exit the script you will have to write it to a file so that it will be available for the next run.
If inside a a script you could move the stuff to do into functions and check the return code you get back from them with return <n>. Outside the functions you read it with $? and could decide by if/then/fi or whatever what to do.
This User Gave Thanks to zaxxon For This Post:
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

Problem with loop within loop

Hi, I work on Ab-initio ETL tool which is based on Unix. I made a small script which has two loop's one with in another. All the functionality is working for the first line of outer loop but when it comes to other lines of outer loop it is throwing error as command not found. Below is the... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

3. 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

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

Problem in quitting/exiting from sqlplus

Hello, This is my first post and I would be very thankful if you can help me. I've already searched in the forum and I've found a very similar thread in wich my problem is solved, but the thread is closed and the solution given in it doesn't work in my shell: 153194-problem-quitting-sqlplus ... (11 Replies)
Discussion started by: JuanPerez
11 Replies

6. 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

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

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 #!/bin/ksh # ************* FUNCTIONS ****************** function1() { #ping test ping $1 2 > /dev/null if ; then ... (13 Replies)
Discussion started by: hcclnoodles
13 Replies

9. 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

10. 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
Login or Register to Ask a Question