what is trap command and how to use it ?

I am a bit interested in this command and would like to share it with u people…

If you want to perform any of the actions on receiving some signal in shell you can do that by “trap” command.

For ex, I would like to create a file when it receives a TERM signal.

I can do this by below way

[terminal]
[root@dhcp208-213 ~]# ls |grep hi =======> There is no file exist in name “hi”

[root@dhcp208-213 ~]# trap “touch hi” TERM ===> I am making a TRAP for the “SIGTERM”

[root@dhcp208-213 ~]# trap ====> This command will show you the TRAP confuigured
trap — ‘touch hi’ SIGTERM
[root@dhcp208-213 ~]# echo $$
32190
[root@dhcp208-213 ~]# kill -TERM 32190
[root@dhcp208-213 ~]# ls |grep hi ====> Now we have “hi” file existing in cwd..
hi
[root@dhcp208-213 ~]#
[/terminal]

I will explain more on this in later article ..:)