exit out of a while loop with error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting exit out of a while loop with error
# 1  
Old 03-03-2009
exit out of a while loop with error

im running a while loop as a file watcher, with incremental counter on the retries..however when the retries reach it's limit i want it exit and echo and error and stop the batch. Im not sure the code i have will do that already...

Here is what i have that works:

#!/usr/bin/ksh
count=0
while [ $count -lt 3 ]
do
count=`expr $count + 1`
echo "Try $count"
RUNDATE=`date "+%h%e"`
filedate=`xyz.txt | awk '{print $6,$7}`
if [ "$Spotfiledate" != "$RUNDATE" ]
then
echo "File's date is $Spotfiledate and today's date is $RUNDATE. They don't match"
echo "I will sleep 30 seconds and try again; 3 tries is my limit"
sleep 30
else echo "Today's file $Spotfiledate is now in."
echo "Sleeping 30 secs to allow it to process."
sleep 30
break
fi

done
# 2  
Old 03-03-2009
Exit with 0 if the try is Ok.

Just put a comment after the loop, meaning all retries have been done.

Code:
#!/usr/bin/ksh
count=0
while [ $count -lt 3 ]
do
   count=`expr $count + 1`
   echo "Try $count"
   RUNDATE=`date "+%h%e"`
   filedate=`xyz.txt | awk '{print $6,$7}`
   if [ "$Spotfiledate" != "$RUNDATE" ] 
   then 
       echo "File's date is $Spotfiledate and today's date is $RUNDATE. They don't match"
       echo "I will sleep 30 seconds and try again; 3 tries is my limit"
       sleep 30 
   else echo "Today's file $Spotfiledate is now in."
       echo "Sleeping 30 secs to allow it to process."
       sleep 30   # maybe this sleep is not necessary
       exit 0
    fi
done
echo "Error, maximum number of retries reach!!"

# 3  
Old 03-03-2009
Quote:
Originally Posted by sigh2010
im running a while loop as a file watcher, with incremental counter on the retries..however when the retries reach it's limit i want it exit and echo and error and stop the batch. Im not sure the code i have will do that already...

The loop will exit when it reaches the maximum. Print your error message after the loop and exit with an error.
Quote:
Here is what i have that works:

Please put code inside [code] tags.
Quote:
Code:
#!/usr/bin/ksh
count=0
while [ $count -lt 3 ]
do
    count=`expr $count + 1`


You don't need an external command to do intergerr arithmetic:

Code:
count=$(( $count + 1 ))

Quote:
Code:
    echo "Try $count"
    RUNDATE=`date "+%h%e"`
    filedate=`xyz.txt | awk '{print $6,$7}`


Are you trying to execute a text file? Or did you mean to pipe it into awk?

Code:
filedate=$(awk '{print $6,$7}' xyz.txt )

But you don't need awk for that:

Code:
read a b c d e f g h < xyz.txt
filedate=$f$g

Quote:
Code:
    if [ "$Spotfiledate" != "$RUNDATE" ]
    then
     echo "File's date  is $Spotfiledate and today's date is $RUNDATE. They don't match"


Where have you defined Spotfiledate?
Quote:
Code:
         echo "I will sleep 30 seconds and try again; 3 tries is my limit"
           sleep 30
     else  echo "Today's file $Spotfiledate is now in."
      echo "Sleeping 30 secs to allow it to process."
       sleep 30
       break
    fi

done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using exit in For Loop - Is this acceptable

Hi Folks - Here is a for loop I've created and I just wanted to see if this was okay practice: for M in NAME1 NAME1 NAME3 do echo "Executing MaxL:" $M >>${_LOGFILE} 2>&1 . ${_STARTMAXLPATH}startmaxl.sh ${_MAINPATH}${_MAXLPATH}$M.mxl _RC=$? if then ... (7 Replies)
Discussion started by: SIMMS7400
7 Replies

2. Shell Programming and Scripting

Exit while loop on execute script

Hi, I have first script which on IR remote command event execute the second script. If the second script is executed, it display echo "timeout expired" after 10s. This works as expected. But I also want to reset timer (increase time) in case if the second script is executed again within 10s. ... (8 Replies)
Discussion started by: armatron
8 Replies

3. Emergency UNIX and Linux Support

For loop exit

Below for loop not exiting. Can someone help? JBOSS_INST_ARGS=01 02 if ; then for i in $JBOSS_INST_ARGS; do /u/jboss-6.1.0.Final/bin/jboss_init_wise$i.sh start; done (8 Replies)
Discussion started by: vino_hymi
8 Replies

4. Shell Programming and Scripting

Strange exit of while loop

This code is used to check for duplicate ip and hostnames in an /etc/hosts file CENTRAL is path to /etc/hosts AWK =awk #check CENTRAL for duplicate ips or hostnames# grep -v "^#" $CENTRAL | $AWK '{ print $1, $2; }' | \ while read ip hostname do if... (5 Replies)
Discussion started by: trimike
5 Replies

5. Shell Programming and Scripting

Loop exit control

Hi I would like to exit the loop below on <Enter> even if it sleeps. Is it possible? while true do my_procedure; sleep 60 done Thanks (7 Replies)
Discussion started by: zam
7 Replies

6. Shell Programming and Scripting

Exit from loop

hi, how to exit from "if" loop?actually i have mutliple "if" conditions, i have to exit from each "if" loop,if it is true...:confused: Please suggest me... (3 Replies)
Discussion started by: sreelu
3 Replies

7. Shell Programming and Scripting

Exit from loop only when selected

I am trying to get my program to exit when the answer to my question is positive, if I am asking if the answers are correct in the entries that the user inputted and the user says no how do I then have it exit? If they say everything is correct then it continue into the program, I think I am close... (2 Replies)
Discussion started by: gumbi17
2 Replies

8. Shell Programming and Scripting

how to exit a while true loop

Hi guys, I'm new to unix but loving it!! BUT this is driving me nuts as i can't work out the best way to do it. I have a while true loop that i use to monitor something. For my own reasons in ths script i have disabled the CTRL C using the trap command. But i want to put in a option to exit... (5 Replies)
Discussion started by: Noob e
5 Replies

9. Shell Programming and Scripting

Method to exit a for loop

Hi All, Can someone let me know how i can exit a for loop without exiting the script itself .... will the break statement work .... please help .... -Regards (2 Replies)
Discussion started by: Rohini Vijay
2 Replies

10. Shell Programming and Scripting

while loop exit

i wrote a while script as part of a huge program. this script, once picked, begins to output data to the person using it. pretty easy, as the person doesn't have to keep typing commands to get the output that the while loop automatically throws out. now, the thing is, while this while-script... (3 Replies)
Discussion started by: Terrible
3 Replies
Login or Register to Ask a Question