Link to home
Start Free TrialLog in
Avatar of msnr
msnr

asked on

controlling ^C and ^D characters in shell script

Hi,

I have a script like this

#!/bin/sh
echo "Enter your id"
read id
echo $id



When i execute above script it will prompt for entering the id,  then if i press ^C  or ^D it will come out of script.
I want to restrict this, even if i press ^C or ^D it should not allow me to come out script.

how to do this.

msnr
Avatar of yuzh
yuzh

use:
   "trap" statement to do the job.

   eg:
   (For ksh script:
    trap "command" 0 1 2 15
    where: 0 -- shell exit, 1-- hangup
           2-- interupt     3--quit
           15 terminate

    the "command" is the job you want to do before terminated the script.

    eg. you can define a function in a ksh script, like the
followings:

    cleanup ()
{
      ec=$1

  case "$ec"
  in
     "$USAGE_EC")  Log "Usage:\t$PROG DeviceName" ;;
     "$NOMNT_EC")  Log "Error:\t$PROG: Invalid device name" ;;
     "$NOTAP_EC")  Log "Error:\t$PROG: ${DEVICE} is not available or no tape" ;;
     "$LISTE_EC")  Log "Error:\t$PROG: failed to create home list for tarring";;
     "$NOTAR_EC")  Log "Error:\t$PROG: tar command failed." ;;
     "$NOLOG_EC")  Log "Error:\t$PROG: could not initialise log file." ;;
     "$TRAPP_EC")  Log "Error:\t$PROG: abnormal program termination" ;;
     *) ;;
  esac

  mt -f $DEVICE rewind >/dev/null 2>&1
    mail ${SUPERVISOR} < $LOGFILE

  trap '' 0 1 2 15

  exit "$ec"
}                                                                                                                       AND put the following statement in the beginning of your script, after your var declearations.    
                             
# Trap on exit/interrupt/break to clean up
#
  trap "cleanup $TRAPP_EC"  0 1 2 15

  The syntax for sh is similar to ksh, as you can define function with ksh script, I perfer to use ksh


                                                   


 
The above example is from one of my backup script. the cleanup function, send me an email about the backup status before it is terminated.
Avatar of msnr

ASKER

yuzh,

can you give me trap script for my example...
in bash...


Here's the example for you:

#!/usr/bin/bash
# function
mytrap()
{
echo "Enter your id"
read id
echo $id
trap '' 0 1 2 15

}
# mail
trap "mytrap"  0 1 2 15


echo "this is a testscript"  
#===============================
# please use which bash to frind out where is your bash
# and modify the first line of this script

cheers!
==========
yuzh                                            
Sorry I make a typing mistake.
#"mail" suppose to be "Main", your script main body, in this case, it just do a function call"

The Main body should look like:


#main
trap "mytrap"  0 1 2 15

echo "this is a testscript"  
mytrap
# End
Avatar of msnr

ASKER

Hi Yuzh,
THanks this is working for ^C only....what about ^D  ..?
if i press ^D it is coming out of script.



msnr
ASKER CERTIFIED SOLUTION
Avatar of yuzh
yuzh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of msnr

ASKER

Hi yuzh,

I will accept your answer as solution, but  ^D is working for only once....if i press second time ^D it is coming out of script..do u know why.?


msnr
Unfortunately Ctrl D is system signal for end of input from the top level of the shell, the first time you press ^D it calls the funtion mytrap(), and the second time you press it again, it exit from the the function and terminated the script. (^D is mapped signal 0 or EXIT)

If you only need to trap ^D, there is a way to get around the problem:

you put the following statement in the beginning of the script:
stty intr "^D"
# intr was map to ^C by default, if you use the above statement, you'll not be able to trap ^C

put the following statement inside the function:
stty intr "^C"
#make the intrrupt sigal back to the system default

I believe that this get arroud is not a REAL solution.