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

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?

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

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.

1 Like