Once the trap catches the signal, there are three ways to react:
(1) Execute a procedure to process this signal
(2) The default action to accept the signal
(3) Ignoring this signal
the trap provides three basic forms for the above three ways:
The first form of the trap command executes the command string in double quotation marks when the shell receives a signal of the same value in the Signal list list.
trap ' commands ' signal-list
trap "Commands" Signal-list
to restore the default operation of the signal, use the second form of the trap command:
Trap Signal-list
the third form of trap command allows to ignore the signal
trap "" Signal-list
Third, note:
(1) The signal 11 (paragraph violation) can not be captured, because the shell itself needs to capture the signal to the memory of the dump.
(2) in the trap can define the processing of signal 0 (in fact, there is no such signal), the shell program at its termination (such as the execution of the exit statement) when the signal is emitted.
(3) After capturing the signal specified in the signal-list and executing the corresponding command, if these commands do not terminate the shell program, the shell program will continue to execute the command following the command executed when the signal is received , which will
it is easy to cause the shell program not to terminate.
also, in a trap statement, single and double quotes are different, and when the shell first encounters a trap statement, the command in commands is scanned again. If commands is enclosed in single quotes, then the shell will not replace the variables and commands in the commands, otherwise the variables and commands in commands will be replaced with the specific values at that time.
Four, example: Ignore Ctrl + C:
In some cases, we do not want our shell scripts to be interrupted at runtime, for example, we write a shell script is set to a user's default shell, so that the user into the system only to do a certain work, such as database backup, we do not want users to use CTRL + C and so on into the shell state, do what we do not want to do. This uses signal processing.
kill-l can list the signal names of the system as follows:
# kill-l
1) SIGHUP 2) SIGINT 3) Sigquit 4) Sigill
5) SIGTRAP 6) SIGABRT 7) Sigbus 8) SIGFPE
9) SIGKILL) SIGUSR1 SIGSEGV) SIGUSR2
sigpipe) sigalrm SIGTERM) SIGCHLD
sigcont) SIGSTOP sigtstp) Sigttin
Sigttou) Sigurg sigxcpu) Sigxfsz
() sigvtalrm) sigprof sigwinch) SIGIO
SIGPWR) Sigsys sigrtmin) sigrtmin+1
sigrtmin+2) sigrtmin+3 (sigrtmin+4) sigrtmin+5
sigrtmin+6) sigrtmin+7 sigrtmin+8) sigrtmin+9
(sigrtmin+10) sigrtmin+11 () sigrtmin+12) sigrtmin+13
sigrtmin+14) sigrtmin+15 SIGRTMAX-14) SIGRTMAX-13
SIGRTMAX-12) SIGRTMAX-11 SIGRTMAX-10) SIGRTMAX-9
SIGRTMAX-8 ) (SIGRTMAX-7) SIGRTMAX-6) SIGRTMAX-5
SIGRTMAX-4 ) (SIGRTMAX-3) SIGRTMAX-2) SIGRTMAX-1
) Sigrtmax
usually we need to ignore the signal has four, namely: HUP, INT, QUIT, TSTP, namely signal 1, 2, 3, 24 use such statement can make these interrupt signal is ignored:
trap "" 1 2 3 24 or trap "" HUP INT QUIT tstp
Use trap:1 2 3 24 or trap HUP INT QUIT TSTP to make it reply to the default value.
stty-a can be used to list the interrupt signal corresponding to the keyboard, respectively, after executing the above command, run
tail-f/etc/passwd
then try to break with the keyboard and try the difference between the two cases (default and ignore).
more convenient is that we can use a trap in the shell to define our own signal handlers, as in C with signal, such as:
trap "Echo ' GO away '" INT
Trap captures signals in the shell