Operating System - HP-UX
1843874 Members
5866 Online
110225 Solutions
New Discussion

User defined key for interrupting a infinite loop script !

 
Chris Fung
Frequent Advisor

User defined key for interrupting a infinite loop script !

Hi all,

Any idea on implementing a user defined key e.g. "q" to interrupt a ever running while loop script ?

The reason for my question is that I want to write a script to continuosuly check some process until the user click on the "q" in the keyboard.

Appreciated it if you can give me some hints.

Cheers,

Chris
5 REPLIES 5
harry d brown jr
Honored Contributor

Re: User defined key for interrupting a infinite loop script !

What shell do you plan on using? Or will you be using perl?

live free or die
harry
Live Free or Die
Bonny Jose_1
Advisor

Re: User defined key for interrupting a infinite loop script !

Chris,

Why dont you use the unitl loop for this purpose?

-Bonny-
let us make the world
Jean-Luc Oudart
Honored Contributor

Re: User defined key for interrupting a infinite loop script !

Hope this help :
============================================
#!/bin/sh

trap 'echo bye && stty intr ^C && exit' 2
stty intr q
while [ true ]
do
date
sleep 1
done
============================================
I changed the interruption to the letter "q" and back to ^C when we quit the program.

Jean-Luc
fiat lux
Chris Fung
Frequent Advisor

Re: User defined key for interrupting a infinite loop script !

Dear all,

I would like to use Bourn shell.

Regards,

Chris,
Jean-Luc Oudart
Honored Contributor

Re: User defined key for interrupting a infinite loop script !

POSIX shell is fine, the good "old" Bourne shell has to go.
I don't see the problem.
sh Bourne-shell command programming language and commands
interpreter residing in file /usr/old/bin/sh. Can
execute commands read from a terminal or a file. This
shell lacks many features contained in the POSIX and
Korn shells. The Bourne shell will be obsoleted.
Users are strongly encouraged to switch to the POSIX
shell. The Bourne shell will still be available as
/usr/old/bin/sh, for those users have to use it.

Jean-Luc
fiat lux