Loop exit control


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop exit control
# 1  
Old 01-09-2011
Loop exit control

Hi

I would like to exit the loop below on <Enter> even if it sleeps. Is it possible?
Code:
              while true
               do
                  my_procedure;
                  sleep 60
               done

Thanks

Last edited by jim mcnamara; 01-09-2011 at 10:38 PM.. Reason: Please use code tags
# 2  
Old 01-09-2011
Are you using the bash shell? use read -t
Code:
while true
do
 my_procedure;
 read -t 60  dummy
done

# 3  
Old 01-10-2011
I am using ksh. I will look for similar info there in man pages. But if you know pls let me know. Thankyou
# 4  
Old 01-10-2011
One technique in "ksh" is to use a flag file.

Code:
while true
do
       my_procedure
       if [ -f /tmp/my_stop_flag ]
       then
                 rm /tmp/my_stop_flag
                 break
       fi
       sleep 60
done

To stop the loop.
touch /tmp/my_stop_flag

Also note that it is never necessary or desirable to end a "ksh" line in a semi-colon.
# 5  
Old 01-10-2011
Thanks !
# 6  
Old 01-12-2011
The option with stop flag Ok but it will not interupt waiting in progress.
# 7  
Old 01-12-2011
We can reduce the reaction time by checking frequently during the sleep period.

Code:
while true
do
       my_procedure
       for sleep_time in 5 5 5 5 5 5 5 5 5 5 5 5
       do
           if [ -f /tmp/my_stop_flag ]
           then
                 rm /tmp/my_stop_flag
                 break 2
           fi
           sleep ${sleep_time}
       done
done

To stop the loop.
touch /tmp/my_stop_flag

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

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

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

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

6. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: sigh2010
2 Replies

7. Shell Programming and Scripting

for loop control

Hi, I have taken a piece of code from a book, which is working as per the specification. The code.... for entry in * do if then echo $entry fi done The sub-directories present in the current directory will be displayed while executing. ... (3 Replies)
Discussion started by: saravanakumar
3 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