1828197 Members
2355 Online
109975 Solutions
New Discussion

Shell Script: trap

 
David Peacock
Frequent Advisor

Shell Script: trap

I wrote a menu with shell script.

I am testing and find it is possible to break from the mneu to the shell.

I use:
trap 1 2 3 4 6

This is what I have found.
If the user hits Ctl C, it sends a character to screen.
If the user hits Ctl D, they get logged off the system.

If they hit Ctl C, and then they hit Ctl D, they break to
the unix prompt.

Is there another trap number to add?

veni, vidi, vmstat
5 REPLIES 5
John Palmer
Honored Contributor

Re: Shell Script: trap

Hi,

In order to ignore signals like ctrl C you need to use:
trap '' 2

That is the action on trap is a null string. Your usage of trap simply resets them to their original values. See man sh-posix for more info.

Regards,
John
A. Clay Stephenson
Acclaimed Contributor

Re: Shell Script: trap

First of all,
trap 1 2 3 is very different from trap '' 1 2 3. The former says restore the signal handlers to their default vaues while the latter says ignore the signal.

Next, you are making some assumptions about Cntl-C and Cntl-D, etc. that are unwarranted. Who is to say what key (if any) generates a SIGINT. You need to also use stty -a to examine these values. One technique that you can use is to use stty to set the intr and quit keys to impossible values so that they can never generate the signals. If you do this, make ceratin that you restore the stty settings to their originals on exit because stty changes affect all processes connected to that tty (or pty port) - not just the current process.
If it ain't broke, I can fix that.
David Peacock
Frequent Advisor

Re: Shell Script: trap

Correction:

I use:

trap " " 1 2 3 4 6

veni, vidi, vmstat
John Palmer
Honored Contributor

Re: Shell Script: trap

Don't include a space in your trap string either.

You MUST have a null string, either two quotes "" or two apostrophes ''.

Regards,
John
RedLetter
Advisor

Re: Shell Script: trap

A simpler answer may be;
Depending on what your script is doing, and how the script is to be used. You might be able to create a wrapper script, that calls your menu script.

#!/usr/sbin/ksh
/home/my_script
exit
Wondeful, never had it so good...