Solved

controlling ^C  and ^D  characters in shell script

Posted on 2002-06-14
9
373 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

910 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now