Use of TRAP Command

Hi,

I would like to know the use of TRAP command. I am very new to the UNIX environment. I have just started learning the basic. So please teach me in a very simple way to understand.

Also i would like to know the use of following command:

trap 'dialog --msgbox "Script Aborted1" 6 50 ; error exit' 1 2 3 15..

Please let me know wat the numbers shown in the above example does mean??

Thanks in advance.

Regards
Deepakh.

trap is a shell builtin which executes the command when the shell receives signals. In your example, the command would translate to 'dialog --msgbox "Script Aborted1" 6 50 ; error exit'. The signals translates to 1, 2, 3 and 15.

Hence whenever your script receives any of the signal 1, 2, 3 or 15, then a popup comes up with the message "Script Aborted1" and the script exits.

From man 7 signal, this is what the numbers stand for

       Signal     Value     Action   Comment
       -------------------------------------------------------------------------
       SIGHUP        1       Term    Hangup detected on controlling terminal
                                     or death of controlling process
       SIGINT        2       Term    Interrupt from keyboard
       SIGQUIT       3       Core    Quit from keyboard
       SIGILL        4       Core    Illegal Instruction
       SIGABRT       6       Core    Abort signal from abort(3)
       SIGFPE        8       Core    Floating point exception
       SIGKILL       9       Term    Kill signal
       SIGSEGV      11       Core    Invalid memory reference
       SIGPIPE      13       Term    Broken pipe: write to pipe with no readers
       SIGALRM      14       Term    Timer signal from alarm(2)
       SIGTERM      15       Term    Termination signal
       SIGUSR1   30,10,16    Term    User-defined signal 1
       SIGUSR2   31,12,17    Term    User-defined signal 2
       SIGCHLD   20,17,18    Ign     Child stopped or terminated
       SIGCONT   19,18,25            Continue if stopped
       SIGSTOP   17,19,23    Stop    Stop process
       SIGTSTP   18,20,24    Stop    Stop typed at tty
       SIGTTIN   21,21,26    Stop    tty input for background process
       SIGTTOU   22,22,27    Stop    tty output for background process

The signals SIGKILL and SIGSTOP can not be trapped.

Thanks Vino.

That was really useful. :slight_smile: