Wednesday, September 18, 2013

Trapping Signals of Terminal in Linux


It frequently happens that while programming either by designing or by accidently the control falls in an indefinite loop. There has to be a way of getting out of such indefinite loops. This facility is provided by the programming environment. Generally we use Ctrl + C to end a process, but this can be trapped using a UNIX shell command “trap”. There are signal numbers for various signals as shown in table below:
 
Table 1. Signals In Unix
We can see a basic shell code which is used to trap the signals 1, 2, 3 (from Table 1).
Example:
trap "" 1 2 3
figlet terminal
figlet locked
                      while true
                      do
                                  echo "Enter Password:"
                                  stty -echo
                                  read password
                                  stty sane
                                  if [ "$password" = "dinesh" ]
                                  then
                                          break
                                  else
                                 echo "Wrong Password, Please Enter Again: "
                                 fi
                      done
OUTPUT:
Figure 1. Output
In the above program we can see various commands such as,
1. figlet: Display large characters made up of ordinary screen characters.
2. stty: Change and print terminal line settings.
For more information about the commands use the manual pages.