[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

controlling ^C  and ^D  characters in shell script

Posted on 2002-06-14
9
Medium Priority
?
391 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
[X]
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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 300 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

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…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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 get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses

650 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