Once the trap captures 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
Two. The trap provides three basic forms for the above three ways:
The first form of the Trap command, when the shell receives a signal of the same value in the Signal list list, performs a dual
The command string in quotation marks.
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
Attention:
(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 be defined in the processing of signal 0 (actually without this signal), the shell program at its termination (such as
This signal is emitted when the exit statement is executed.
(3) After capturing the signal specified in the signal-list and executing the corresponding command, if these commands do not
If the shell program terminates, the shell program will continue executing the command following the command that was executed when the signal was 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 commands in the commands will be 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 the commands will be used for the specific values
Kill-l can list the signal of the system
Usually we need to ignore the signal has four, namely: HUP, INT, QUIT, TSTP, namely signal 1, 2, 3, 24
Use such statements to make these interrupt signals 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, and then try to break with the keyboard, try the difference between the two cases (default and ignore).
More conveniently, we can use a trap to define our own signal handlers in the shell.
Processing of the trap capture signal in the shell