The Trap command is used to specify the action to be taken after the signal is received;
The parameters of the Trap command are divided into two parts, the first part is the action to be taken when the specified signal is received, and the latter part is the signal name to be processed. and you must specify the trap command before the part of the code that you want to protect.
Format: Trap Commands signal-list
Example: Trap "Cp-f/etc/resolv.conf.bak/etc/resolv.conf;exit" INT
..........................................
Signal (SIGINT) |
Description |
HUP (1) |
Hangs, usually caused by a terminal drop or user exit |
INT (2) |
Interrupt, usually caused by pressing CTRL + C key combination |
QUIT (3) |
Exit, usually caused by pressing the ctrl+/key combination |
ABRT (6) |
Abort, usually caused by some serious execution error |
ALRM (14) |
Alarms, typically used to process timeouts |
Term (15) |
Terminated, usually sent at system shutdown |
INT is the interrupt signal (Ctrl-c,sco UNIX is the "Del" key in Linux)
The trap is the capture signal, which is associated with the provided ARG, which performs the action specified by ARG when a signal is captured.
#!/bin/bash
Trap "ECHO-E \" \nreceive int\n\ "; exit" int
While:
Do
((i++))
Done
Since it is a dead loop and will not exit, after pressing the CTRL-C key, the program will display, receive int, and return the $ status. When Arg is empty, the signal indicated later is ignored
Trap-p
The command associated with the signal can be displayed. Please correct me.
This article is from the "North Ice--q" blog, please be sure to keep this source http://beibing.blog.51cto.com/10693373/1894457
Linux Trap (SNAP) command