Operating System - HP-UX
1834437 Members
2460 Online
110067 Solutions
New Discussion

Exiting from a Shell Script

 
Mike Ingram
Occasional Advisor

Exiting from a Shell Script

I am using a trap command in a shell script, to prevent users from pressing Ctrl-C to get to the command line. This works as planned, but if a user presses Ctrl-X as well as Ctrl-C, then they access the command line. Any suggestions on how to prevent this happening?
Don't be shy to ask if you don't know the answer
5 REPLIES 5
James Murtagh
Honored Contributor

Re: Exiting from a Shell Script

Hi Mike,

I think you'll need to find out what signal Ctrl-X is sending. Use :

# stty -a

from the users shell. You can then map this to the trap number using :

# kill -l

Add this to your shell script trap and hopefully this will work.

cheers,

James.
Rob_132
Regular Advisor

Re: Exiting from a Shell Script



1. vi /usr/include/sys/signal.h
2. search for ???signal numbers???


trap ' || >???
<2nd line of script>
etc.

The most common signals to trap are 1,2,3,9,11,14,15


On 10.20, the signal numbers are:

/* Signal numbers */

# define _SIGHUP 1 /* floating point exception */
# define SIGINT 2 /* Interrupt */
# define _SIGQUIT 3 /* quit */
# define SIGILL 4 /* Illegal instruction (not reset when
caught) */
# define _SIGTRAP 5 /* trace trap (not reset when caught) */
# define SIGABRT 6 /* Process abort signal */
# define _SIGIOT SIGABRT /* IOT instruction */
# define _SIGEMT 7 /* EMT instruction */
# define SIGFPE 8 /* Floating point exception */
# define _SIGKILL 9 /* kill (cannot be caught of ignored) */
# define _SIGBUS 10 /* bus error */
# define SIGSEGV 11 /* Segmentation violation */
# define _SIGSYS 12 /* bad argument to system call */
# define _SIGPIPE 13 /* write on a pipe with no one to read it */
# define _SIGALRM 14 /* alarm clock */
# define SIGTERM 15 /* Software termination signal from kill */
# define _SIGUSR1 16 /* user defined signal 1 */
# define _SIGUSR2 17 /* user defined signal 2 */
# define _SIGCHLD 18 /* Child process terminated or stopped */
# define _SIGCLD _SIGCHLD /* death of a child */
# define _SIGPWR 19 /* power state indication */
# define _SIGVTALRM 20 /* virtual timer alarm */
# define _SIGPROF 21 /* profiling timer alarm */
# define _SIGIO 22 /* asynchronous I/O */
# define _SIGPOLL _SIGIO /* for HP-UX hpstreams signal */
# define _SIGWINCH 23 /* window size change signal */
# define _SIGWINDOW _SIGWINCH /* added for compatibility reasons */
# define _SIGSTOP 24 /* Stop signal (cannot be caught or ignored) */
# define _SIGTSTP 25 /* Interactive stop signal */
# define _SIGCONT 26 /* Continue if stopped */
# define _SIGTTIN 27 /* Read from control terminal attempted by a
member of a background process group */
# define _SIGTTOU 28 /* Write to control terminal attempted by a
member of a background process group */
# define _SIGURG 29 /* urgent condition on IO channel */
# define _SIGLOST 30 /* remote lock lost (NFS) */
# define _SIGRESERVE 31 /* Save for future use */
# define _SIGDIL 32 /* DIL signal */
# define _SIGXCPU 33 /* CPU time limit exceeded (setrlimit) */
# define _SIGXFSZ 34 /* CPU file size limit exceeded (setrlimit) */

Hope this helps!

Rob
derek b smith_1
Regular Advisor

Re: Exiting from a Shell Script

From Robs answer about the header file signal.h I am not understanding the syntax of the trap command. Here is what I attempted...

trap `dfrag |echo "trapping signal 15 for PID dfrag" 15`

It started dfrag but I did not see my trap displayed? Is this corect? How do I trap this PID of dfrag?

NOTE : dfrag is an executable

thanks
derek
curt larson_1
Honored Contributor

Re: Exiting from a Shell Script

the syntax for the trap command is
trap [action condition]

you should be doing something like this

#set trap
trap "print \"caught signal\"" INT QUIT TERM

dfrag

#unset trap
trap - INT QUIT TERM

# INT QUIT TERM are signal numbers 2,3, and 15
Bill Hassell
Honored Contributor

Re: Exiting from a Shell Script

If I understand correctly, you want a CTRL-C to be ignored (CTRL-X is not normally defined as an stty action in HP-UX so use stty -a to see what it does). To ignore a trap signal such as CTRL-C, you can take a hint from /etc/profile:

# Ignore HUP, INT, QUIT now.
trap "" 1 2 3

...

trap 1 2 3

The first trap disables trap signals 1 2 and 3 and then at the end, re-enables the traps. So during /etc/profiel, CTRL-C has no effect and is ignored. Now if you want your script to exit, there are two choices: make the script the user's shell, then an exit leaves the session completely. Otherwise, you'll need to exec the shell script so that the script is run by the current shell, not a subshell.


Bill Hassell, sysadmin