Operating System - HP-UX
1829749 Members
1455 Online
109992 Solutions
New Discussion

Trapping Ctrl C signal...

 
Purusa
Frequent Advisor

Trapping Ctrl C signal...

Hi all,

I have a scenario whereby there are 2 scripts- script A and B.
Script A calls B. Both script A as well as B have the inbuilt traps i.e. trap "" 2 3 15 .Script B has commands like swlist, swinstall etc.
I invoke script A. It calls script B and when the execution is going on in script B, I invoke Ctrl +C and it gives me following messages:

WARNING: Exiting due to keyboard interrupt.
WARNING: Exiting due to keyboard interrupt.
/abc/xyz/pankaj[47]: test: Specify a parameter with this command.

Same thing does not happen in case execution is continuing in script A. Does anyone have a clue as to why the trap for Ctrl + C is not working in script B. Is it something to do with swinstall, swlist commands? If yes, then what is the workaround for the same?

Also if I continously press Ctrl + C then WARNING message keeps on popping and normal functioning of script B is hampered.

Thanx for your responses in advance...
Regards,
Pankaj
A deep chasm can't be crossed in two steps
5 REPLIES 5
Nicolas Dumeige
Esteemed Contributor

Re: Trapping Ctrl C signal...

Hello,

ksh provide the trap function to deal with signal.

Exemple :
#!/bin/ksh

gotcha() {
echo 'Noticed you wanted to exit shell'
echo 'cleaning stuff and leaving'
exit 0
}

trap "gotcha" 2
sleep 10
exit 0

Cheers

Nicolas
All different, all Unix
Mark Grant
Honored Contributor

Re: Trapping Ctrl C signal...

This bit suggest a script error at line 47. Probably a variable that is null or syntax error in the test.

/abc/xyz/pankaj[47]: test: Specify a parameter with this command

However, if "swlist" et al redefine the action to take on SIGINT which they probably do, there isn't much you can do about it really. I would be surprised if the "swlist" program didn't redefine SIGINT becuase most people don't run it from a script taht traps SIGINT itself.

Maybe try the other approach, i.e. redefining the interrupt key to something bizarre using "stty" for the duration of the script.
Never preceed any demonstration with anything more predictive than "watch this"
Purusa
Frequent Advisor

Re: Trapping Ctrl C signal...

Hi,
Thanx for the solutions.
The script error is not having an error as without the interrupt signal it works fine.
Also how to redefine using "stty"??? Will it work if swlist is already handling the interrupt signal???
Any other solution for the scenario...

Regards,
Pankaj
A deep chasm can't be crossed in two steps
Stuart Browne
Honored Contributor

Re: Trapping Ctrl C signal...

You are successfully trapping the INT signal (2) during the execution of the script.

However, swlist/swinstall etc. have their own INT signal trapping, and that's getting past your script's trap.

Because the INT is breaking out of whatever the swlist etc. are doing, and you're expecting a result to be thrown into a VAR (it seems), the VAR is empty, thus causing the error on line 47.

Possibly need to do some error checking prior to doing the needed test on line 47 (i.e. see if the output of the swlist that you're capturing actually has anything in it).
One long-haired git at your service...
Mark Grant
Honored Contributor

Re: Trapping Ctrl C signal...

Redefine the key thus

stty intr ^@

It is important to note that the "^@" here is not the CTRL and @ key together. It is the ^ and then the @
Never preceed any demonstration with anything more predictive than "watch this"