1829092 Members
2758 Online
109986 Solutions
New Discussion

Re: Trapping Ctrl-C

 
SOLVED
Go to solution
MikeL_4
Super Advisor

Trapping Ctrl-C

I have a script that is called to force user to reset there password and I do not want them to be able to Ctrl-C out of it.
I believe a SIGINT (2) is to trap the Ctrl-C but when I code: trap 2 in the script it still allows me to break out of it with Ctrl-C.
If possible I would also like it to display, Control-C not allowed if they do try it and reissue the password command for them....
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor
Solution

Re: Trapping Ctrl-C

Hi Mike:

A trap in your script like this should work:

trap 'echo Ctrl_C not allowed!' INT

Do 'stty a' to confirm that the Control_C sequence is mapped to SIGINT. You should see:

intr = ^C;

Regards!

...JRF...
RAC_1
Honored Contributor

Re: Trapping Ctrl-C

trap "" 1 2 3 to ignore the signals-123
trap 1 2 3 to trap the signals.
There is no substitute to HARDWORK
Stuart Abramson_2
Honored Contributor

Re: Trapping Ctrl-C

This is NOT what you asked, but maybe it will help. It's how to write a script to trap a "kill" command:

#!/usr/bin/ksh
#
# ~stuart/ksh/traptest.ksh SDA 09/25/97
#
# This script loops around until it "catches" a signal, sent by a
# kill command. We will trap SIGINT and SIGQUIT
#
# We launch this guy by typing:
#
# ksh traptest.ksh &
#
# We send the trap by typing:
#
# kill -SIGINT
#
USAGE="Usage: traptest.ksh"
#

trap 'print "Received INT; c = $c"' INT
trap 'print "Received QUIT; rt = $rt"' QUIT

integer c=0
integer rt=0

while ((c<200000))
do
((c=c+1))
((rt=rt+c))
done

print "The final answer is c = $c; rt = $rt "


e. OUTPUT:

$ ksh traptest.ksh &
[1] 5345
$ jobs
[1] + Running ksh traptest.ksh &
$ kill -SIGINT %1
$ Received INT; c = 66551

$ kill -SIGINT %1
$ Received INT; c = 110368
$ kill -SIGQUIT %1
$ Received QUIT; rt = -883017042

$ The final answer is c = 200000; rt = -1474736480

[1] + Done ksh traptest.ksh &