How to trap interrupts in the bash shell

#!/bin/bash
 
myCleanup() {
  rm -f /myapp/tmp/mylog
  return $?
}
 
myExit() {
  echo -en "\n*** Exiting ***\n"
  myCleanup
  exit $?
}
 
trap myExit SIGINT
 
# main loop
while true
do
    echo -n "Enter your name: "
    read x
    echo "Hello $x"
done

Ref: http://mariusvw.com/2010/05/18/how-to-trap-catch-keyboard-interrupt-in-bash-on-linux-freebsd/

Discussion

Enter your comment
If you can't read the letters on the image, download this .wav file to get them read to you.
 
blog/how_to_trap_interrupts_in_the_bash_shell.txt · Last modified: 2011/02/16 20:15 by brb
Back to top
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0