how to exit a while true loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to exit a while true loop
# 1  
Old 08-25-2007
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 the script using the exit command.

So i get it to display the info for say 30 seconds then it loops, then displays the new info. While it is waiting for 30 seconds i want the option to exit the script by pressing say the x key. As im new to unix not sure how to do this, i either work in ksh or bash. Pleeease give me an idea how to do this. Smilie
# 2  
Old 08-25-2007
Quote:
Originally Posted by Noob e
i have disabled the CTRL C using the trap command.
Hm, want to quit but trap the Ctrl-C.

Fortunately their is a SIGQUIT signal you could use. (Normally ctrl+\)
# 3  
Old 08-25-2007
Been told that im not allowed to use the CTRL C to exit the scripts, so i removed the option. (Don't ask me why but the admin was suggesting it caused issues) I want to press any key while the loop is running to exit. So the script is running the loop but also looking for a key press to exit, tried exiting the script using CTRL \ didnt work. IF SIGQUIT is an answer how can i use it in the way i want?
# 4  
Old 08-25-2007
Code:
#! /usr/local/bin/bash
count=0
while : ; do
        echo count = $count
        ((count=count+1))
        read -t 10 && break
done
echo exited while loop
exit 0

This uses the -t option to the read command in bash. The "count" is just to have something that changes to display with each iteration. If the user presses return within 10 seconds, the read succeeds, otherwise it fails. (If it succeeds, we just discard the data that was read since no variable is used with the read.) -t works with bash, but most versions of ksh do not have it.
# 5  
Old 08-25-2007
#you can use break to quit the loop.
#you can quit the script itself by using exit once you reach the condition to quit the whole!!

If you want to run it for particular time in seconds... here we go
#!/bin/sh
secs=$1
start=`date +%s`
end=$((start+$secs))
while : ; do
if [ `date +%s`-eq $end ]; then
break
fi
.
.
. here goes your monitoring logic
done
#here you can write the rest of the script or simply put exit 0

---------now you can run the script passing the arguement as no. of secs you want ti to run!!

-ilan

Last edited by ilan; 08-25-2007 at 10:52 AM..
# 6  
Old 08-26-2007
Thanks guy for the advice i ended up using.....

answer="No";read -p "Do you want to exit? Answer y to exit. " -t 30 answer;echo " Your answer is $answer";
if [ "$answer" = "y" ];then
echo "you have chosen to exit."
break
else
echo ""


giving them a 30 second window to exit the script or the while true just continues.. works a treat.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While true loop stopped

Hello Team, In my shell script, it stopped without killing it. Could you please help why ? Below is the code : get_dpi44_currentFiles() { truncate --size 0 $dpi_44_fixed_currentFileList if ssh $dpi_srvr_44 -p 2222 "ls $dpi_44_sourceFilesDir/fixed | grep '\.tgz$'" >... (5 Replies)
Discussion started by: sadique.manzar
5 Replies

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

3. Shell Programming and Scripting

While loop true problem

hi, i am new in bash scripting. i am trying to write a Bash script to monitor how many process are running in my Redhat6 Linux machine. example : let say there must be always 5 process from the same kind that must run and if one off them fails down the script must wake up another process... (2 Replies)
Discussion started by: zigizag
2 Replies

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

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

6. Shell Programming and Scripting

loop until true

Hi I need help with a script to loop unitl the statement is true done some thing like this until if then cp filename filename.anto fi done Regards, (3 Replies)
Discussion started by: antointoronto
3 Replies

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

8. Shell Programming and Scripting

why isn't the exit status true?

the code: do } ] || mkdir -p ${mk_backup_dir} && echo "ERROR: release backup directory creation failed -${mk_backup_dir}" && exit done echo "INFO: Backup directories created" the result: mkdir: "/cm/uat_releases/riab/uat/2345": Permission denied ERROR: release backup directory... (5 Replies)
Discussion started by: mjays
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