1827280 Members
2275 Online
109717 Solutions
New Discussion

Re: korn shell

 
SOLVED
Go to solution
Sathish C
Frequent Advisor

korn shell

Hi
Is there any way to trap keys including interrupts ( like ^C,^D etc) though korn shell commands ? . Is so can someone tell me how ?

I expect answers apart from
set trap < command > signal
Some cause happiness wherever they go; others, whenever they go
10 REPLIES 10
Elmar P. Kolkman
Honored Contributor

Re: korn shell

I am not sure I understand what you want to do...
If you want to assign commands to the keys, you'll be stuck with the trap command.
But if you want to be able to enter those characters, you can do it by using stty and assign other characters to the interrupt and EOF signals:
stty EOF ^? INTR ^?
for instance.
Or you could enter them by first enter a -V.
Every problem has at least one solution. Only some solutions are harder to find.
Sathish C
Frequent Advisor

Re: korn shell

Ok. for instance there ia an application running , if the user iterrupt the run by pressing ^C or ^D , i should be able to not to allow it .
Some cause happiness wherever they go; others, whenever they go
Elmar P. Kolkman
Honored Contributor

Re: korn shell

In that case you could either make the interrupts not accessible (linking them to keys not on the keyboard) with stty or using the trap command.
Or you could ignore the signals from within the application. And how that can be done depends on the language the application is written in.
Every problem has at least one solution. Only some solutions are harder to find.
Mic V.
Esteemed Contributor
Wodisch
Honored Contributor

Re: korn shell

Hi Sathish,

what you are actually talking about are "signals", in your example produced by the TTY drivers upon certain key-presses.
But there are more signals available than just:
- SIGINT (ususally CTRL-C, ignore it with "trap : 2")
and
- SIGQUIT (ususally something like CTRL-\i, ignore it with "trap : 3"):

Those are:
- in case of serial terminal lines: the BREAK-key (it can generate a SIGINT; turn it off with "stty -brkint")
- SIGHUP (usually sent to all your processes when your shell exits; ignore it with "trap : 1" or "nohup")
- SIGWINCH (sent by your local window manager; ignore it using "trap : " and the number as shown by "kill -l")

To "unset" special keys you'll have to assign an impossible key, like "^@" (which would be the codenumber 0, and cannot be generated by PC-keyboards).

Happy new year,
Wodisch
Wodisch
Honored Contributor

Re: korn shell

Hi Sathish,

what you are actually talking about are "signals", in your example produced by the TTY drivers upon certain key-presses.
But there are more signals available than just:
- SIGINT (ususally CTRL-C, ignore it with "trap : 2")
and
- SIGQUIT (ususally something like CTRL-\, ignore it with "trap : 3"):

Those are:
- in case of serial terminal lines: the BREAK-key (it can generate a SIGINT; turn it off with "stty -brkint")
- SIGHUP (usually sent to all your processes when your shell exits; ignore it with "trap : 1" or "nohup")
- SIGWINCH (sent by your local window manager; ignore it using "trap : " and the number as shown by "kill -l")

To "unset" special keys you'll have to assign an impossible key, like "^@" (which would be the codenumber 0, and cannot be generated by PC-keyboards).

Happy new year,
Wodisch
Wodisch
Honored Contributor

Re: korn shell

sorry for the re-transmission of my posting :-(
Sathish C
Frequent Advisor

Re: korn shell

Hi Wodisch
Thank you the for your responce. The application that I have is a one hell of long smart korn shell with bit of Tcl/Tk , its basically a menu system that branches into lots of sub-menues and so on , it is designed to handle all the charcters and numbers during the input validation but for any interrupts via ^C,^D or any other combinations I am not able to control them . For instance I do not want the user to exit the program until the program exits himself . But I have already tried set trap option , bit I amo not sure how to ignore them .

set trap "echo Pls You Can Not Log Out Now":0

But this is not going to redo the real action of the interrupt 0 , but it also does the echo message along with the default action of interrupt 0 .
Some cause happiness wherever they go; others, whenever they go
Elmar P. Kolkman
Honored Contributor
Solution

Re: korn shell

Written out you can solve your problem as shown below. The stty -g is necessary because otherwise your terminal setting would be garbled after the script is ready. With the -g options and later using that output you save and restore the settings.


OLDSTTY=`stty -g`
stty intr ^@
stty eof ^@
stty kill ^@
stty -brkint
trap "echo Please let me run" 0 1 2 3 4 5 6 7 8


stty $OLDSTTY
trap "" 0 1 2 3 4 5 6 7 8
Every problem has at least one solution. Only some solutions are harder to find.
Sathish C
Frequent Advisor

Re: korn shell

Hi
This is cool . I have tried it now , except the echo message that is not getting displayed for instance when I press ^C , else is fine , the signals are getting ignored .


Thank you all
Some cause happiness wherever they go; others, whenever they go