1847694 Members
6256 Online
110265 Solutions
New Discussion

Capturing Ctrl + C

 
SOLVED
Go to solution
Radhe Webster
Occasional Advisor

Capturing Ctrl + C

I have created a script that is called out of a users .profile when they log in. In the script I have:

trap 'print "ENTER INFORMATION REQUESTED!"' INT
trap 'print "ENTER INFORMATION REQUESTED!"' QUIT

And then goes on to ask them for some requested information. For some reason it's not working correctly and ctrl + c breaks out of it. I've tried it with trap "" 1 2 3 and that isn't working either.

Any thoughts? Thanks
6 REPLIES 6
Steve Steel
Honored Contributor

Re: Capturing Ctrl + C

Hi


Use stty to change the intr value at the start and end of script . see stty -a


Why the trap.How is the script used

see
www.shelldorado.com for good shell tips

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Simon Hargrave
Honored Contributor
Solution

Re: Capturing Ctrl + C

The way trap works is, it EITHER ignores the signal (if you use a null argument) OR it runs the command then processes the trap.

eg

trap '' INT

will make it ignore Ctrl-C

trap 'echo test' INT

will make it print the word "test", then exit the program.

You can't have both unfortunately.
Radhe Webster
Occasional Advisor

Re: Capturing Ctrl + C

It's so that our various vendors identify themselves (and we get emailed) when they log onto the system.

The command trap " INT generates syntax errors... should it not be trap "" INT
Simon Hargrave
Honored Contributor

Re: Capturing Ctrl + C

I was using 2 single-quotes. I guess you read this as one double-quote? You can use either a pair of single-quotes or a pair of double-quotes, it doesn't make a difference when using a null string. When specifying a command your use depends on whether you want shell expansion.
Radhe Webster
Occasional Advisor

Re: Capturing Ctrl + C

I've gotten it to work by using stty to change the interrupt value, but trapping it is still not working.
Stuart Browne
Honored Contributor

Re: Capturing Ctrl + C

What shell are you using?

And does it fail when you just use:

trap 'print blah' 2

One long-haired git at your service...