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

i do some thing like this ....

while true
do

echo "input string:\\c"
read a;

echo $a

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

done

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.

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.


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

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.

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.

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.

Doh! I started to post a tip on how to do this in C and I just realized that this can probably all be done with the terminal driver itself and thus can probably be done in a shell script. Maybe even with csh! :wink:

It would be close to the technique used in this thread, except with the values min=0 and time=10.

Oh well... the swtich to c++ was probably still wise.