Operating System - HP-UX
1831099 Members
2513 Online
110019 Solutions
New Discussion

Re: Can't use CTRL-C to exit a script

 
SOLVED
Go to solution

Can't use CTRL-C to exit a script

Hi to all, I've created a menu-based script and I want the user to exit from the script using the available options and not by using CTRL-C. How would I implement this? What snippet of code can I add in my script so that this will be possible?

TIA
8 REPLIES 8
Chris Wilshaw
Honored Contributor

Re: Can't use CTRL-C to exit a script

If you just want to disable CTRL+C, you should add the line

trap "" 2

to the script.

For extra protection (from hangup and quit signals)

use trap "" 1 2 3
RolandH
Honored Contributor

Re: Can't use CTRL-C to exit a script

Put this in your script at the beginning.

stty intr ^-

this will change the interrupt routine to undefined.


Roland
Sometimes you lose and sometimes the others win
T G Manikandan
Honored Contributor

Re: Can't use CTRL-C to exit a script

You can introduced a case statement where you can check for the condition and then exit out from the script.


Thanks

Re: Can't use CTRL-C to exit a script

Sir Chris, will the command also disable CTRL-D? What do you mean when you say that it will also disable hangup and quit signals?

again, thanks.
RolandH
Honored Contributor

Re: Can't use CTRL-C to exit a script

Hi Rodel,

PLEASE, do not waht I suggest before.
It switched off the interrupted routine for the shell you have in wihich you start the script, too. So do that in your script:

trap "" 2



Roland
(NO POINTS ,PLEASE)
Sometimes you lose and sometimes the others win
Chris Wilshaw
Honored Contributor
Solution

Re: Can't use CTRL-C to exit a script

Rodel,

The command won't disable CTRL+D, although this shouldn't matter as the user is in a script, and CTRL+D is normally an exit from a shell.

The hangup and quit signals,(from man kill);

1 SIGHUP Hangup Terminate; can be trapped
2 SIGINT Interrupt Terminate; can be trapped
3 SIGQUIT Quit Terminate with core dump; can be trapped

The Quit signal can be triggered manually using CTRL+\ (another potential way for users to exit the script).

The hangup is more normally sent directly via the kill command (kill -HUP PID)

If you use kill -l (that's a lower case L), it will list all signals that can be sent from the kill command, most of which can be trapped if necessary. More details can be seen in the man pages using

man 5 signal

Re: Can't use CTRL-C to exit a script

Sir Chris, how would I enable again CTRL-C because one option of my script goes to a shell prompt and I can't use CTRL-C to interrupt a running process/command?

again, thanks.
Chris Wilshaw
Honored Contributor

Re: Can't use CTRL-C to exit a script

To re-enable the signal, use

trap - 2, then call your shell.

The - sets the trap for whichever signals you list after it back to their default values.

Chris