The default action taken by your process when it receives any of the signal is immediate termination. The trap command can change the process default action to whatever you specify.
Optional of The trap Command
The Optional Command(s)
optional command(s) part is optional. When it is present, the commands (could be more than one command) are executed whenever your process receives one of the signals you have specified to be trapped.
You can specify more than one signal number to be trapped.
Examples of trap Command
trap "echo I refused to die!” 15
This command executes the echo command and displays the
I refuse to die! message
whenever it receives a simple kill command. However, you script continues.
trap "echo Killed by a signal!; exit” 15
If your process receives a kill command (signal 15), the echo command is executed and shows the
Killed by a signal! message. Next the exit command is executed, which causes your process to terminate.
trap " " 15
No command is specified. Now if your process receives the kill command (signal 15), it ignores it, and the script continues.
Function of the trap Command
The trap command is useful when you want to trap a certain signal in one part of your script and in another part you want the signals not to be trapped.
Resetting the traps
trap " " 2 3 15
The interrupt, quit, and kill commands are ignored, and if any of their keys is pressed, you script keeps on running.
trap 2 3 15
The specified signals are reset. That is to say, the interrupt, quit, and kill keys are restored, and if any of them is pressed, your running script is terminated.
The tput Command
The tput command can set the terminal capabilities.
Examples of tput Command
clear Clears the screen
tput cup 10 20
Positions the cursor to row 10 and column 20
bell=`tput bel`; echo $bell
sounds the terminal bell
s_uline=`tput smul`; echo $s_uline
starts underline display
e_uline=`tput rmul`; echo $e_uline
Ends underline display
sounds the terminal bell; Hello is echoed and underlined; ends underline display
Other Tools and Information
The final piece to understanding shell scripting (and to understanding other people‘s shell scripts) is comprehending the use of command-line tools.
Each of these tools has its own syntax. However, this blog briefly highlights some common tools and includes links to their manual pages for finding additional information about them.
Security Issues Infected Shell Scripts
A brief warning about script security is indicated. A shell script may contain a worm, trojan, or even a virus.
For that reason, never run as root a script (or permit it to be inserted into the system startup scripts in /etc/rc.d) unless you have obtained said script from a trusted source or you have carefully analyzed it to make certain it does nothing harmful.
Various researchers at Bell Labs and other sites, including M. Douglas McIlroy, Tom Duff, and Fred Cohen have investigated the implications of shell script viruses.
They conclude that it is all too easy for even a novice, a
script kiddie,to write one. Here is yet another reason to learn scripting. Being able to look at and understand scripts may protect your system from being compromised by a rogue script.
Shell Scripting Under Windows
Even users running that other OS can run UNIX-like shell scripts. The Cygwin package from Cygnus and the MKS utilities from Mortice Kern Associates add shell scripting capabilities to Windows.