The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash: Exiting while true loop when terminal is not the focus window acclaypool Shell Programming and Scripting 2 02-25-2008 06:02 PM
why isn't the exit status true? mjays Shell Programming and Scripting 5 04-04-2007 09:18 AM
Method to exit a for loop Rohini Vijay Shell Programming and Scripting 2 11-01-2006 05:56 AM
while loop exit Terrible Shell Programming and Scripting 3 07-28-2006 05:20 PM
csh exit while loop on keystroke seg Shell Programming and Scripting 7 12-29-2004 11:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-25-2007
Noob e Noob e is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 5
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.
  #2 (permalink)  
Old 08-25-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by Noob e View Post
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 (permalink)  
Old 08-25-2007
Noob e Noob e is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 5
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 (permalink)  
Old 08-25-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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 (permalink)  
Old 08-25-2007
ilan ilan is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 101
#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 09:52 AM..
  #6 (permalink)  
Old 08-26-2007
Noob e Noob e is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 5
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0