Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

controlling ^C  and ^D  characters in shell script

Posted on 2002-06-14
9
375 Views
Last Modified: 2013-12-27
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
0
Comment
Question by:msnr
  • 6
  • 3
9 Comments
 
LVL 38

Expert Comment

by:yuzh
ID: 7078302
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


                                                   


 
0
 
LVL 38

Expert Comment

by:yuzh
ID: 7078314
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.
0
 
LVL 1

Author Comment

by:msnr
ID: 7080129
yuzh,

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


0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 38

Expert Comment

by:yuzh
ID: 7081630
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                                            
0
 
LVL 38

Expert Comment

by:yuzh
ID: 7081634
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
0
 
LVL 1

Author Comment

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



msnr
0
 
LVL 38

Accepted Solution

by:
yuzh earned 100 total points
ID: 7084988
Hi msnr,

   Here's a newer version of the script, it will trap for both ^C and ^D.

#!/usr/bin/bash
# function
mytrap()
{
echo "Enter your id"
read id
echo $id
}
# Main

trap "mytrap"  0 1 2 3 9 15
 
echo "this is a testscript"
mytrap
exit

# End of the script
# for information about your system signals have a look
# at /usr/include/sys/signal.h file

Cheers!
==========
yuzh



0
 
LVL 1

Author Comment

by:msnr
ID: 7085101
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
0
 
LVL 38

Expert Comment

by:yuzh
ID: 7085488
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.


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question