The UNIX and Linux Forums  


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
how to exit a while true loop Noob e Shell Programming and Scripting 5 08-26-2007 03:44 PM
Method to exit a for loop Rohini Vijay Shell Programming and Scripting 2 11-01-2006 05:56 AM
Need Clean Exit from KSH Status Loop ScottKe Shell Programming and Scripting 2 08-30-2006 05:53 PM
while loop exit Terrible Shell Programming and Scripting 3 07-28-2006 06:20 PM
keystroke trap vasikaran UNIX for Dummies Questions & Answers 1 07-04-2005 09:47 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 12-17-2004
seg seg is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2003
Posts: 260
csh exit while loop on keystroke

#!/bin/csh

I'm using a `while(1)` loop to dispaly real-time information about various files on my system, and I use ^C to exit it when needed. I was hoping there was a way to exit the script on a normal keystroke such as "q". Can someone point me in the right direction? I'm willing to use a different method for looping if needed.

Thanks,
-Seg
  #2 (permalink)  
Old 12-17-2004
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2004
Location: USA
Posts: 511
i do some thing like this ....


while true
do

echo "input string:\c"
read a;

echo $a

if [ $a = "q" ]
then
break ;
fi


done
  #3 (permalink)  
Old 12-23-2004
seg seg is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2003
Posts: 260
That's not really what I'm trying to do, besides that code is for sh, not csh. Anyway, I'm running a loop where there is no prompt for user input, the script clears the screen, displays info, sleeps for 1 second then starts over. I'd like to break out of the loop at any time using a standard key stroke rather than ^C.
  #4 (permalink)  
Old 12-23-2004
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
Use a signal and have your script listen for the signal. The syntax depends on the shell you are using. If you are using ksh, see man ksh. Also search the boards as there are many examples of using signals.

trap "command to run when signal trapped" "list of signals"

trap "" signals # Will trap the signals you define but not allow them to affect your script.

you can get a list of signals by typing kill -l on the command line. Note: You cannot trap signal 9. Your "command to run" can be a function if you so choose.


Code:

User Commands                                             trap(1)

NAME
     trap, onintr  -  shell  built-in  functions  to  respond  to
     (hardware) signals

SYNOPSIS
  sh
     trap [  argument  n  [ n2 ... ]  ]

  csh
     onintr [ -| label ]

  ksh
     *trap [  arg sig  [  sig2 ...  ]  ]

  #5 (permalink)  
Old 12-28-2004
seg seg is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2003
Posts: 260
Thanks for the information. In all fairness I've gone ahead and moved this project to C++ because it is beyond the scope of shell scripting.
  #6 (permalink)  
Old 12-28-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Switching to C is wise, but you could just write a quick utility that sleeps for 1 second unless a key is pressed and returned an appropiate exit code. Then the rest of the project could be in a shell script.
  #7 (permalink)  
Old 12-29-2004
seg seg is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2003
Posts: 260
That is true, but moving to C++ will allow me much more versatility in the program. As my managers would say "moving forward" I will need to add many more functions to it and being just minimally realistic about it, it's going to need to be in something more capable and secure than a 20 line shell script.
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 09:15 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