Solved

controlling ^C  and ^D  characters in shell script

Posted on 2002-06-14
9
372 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

746 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

11 Experts available now in Live!

Get 1:1 Help Now