User Tools

Site Tools


blog:how_to_trap_interrupts_in_the_bash_shell

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/

~~LINKBACK~~

Discussion

Enter your comment. Wiki syntax is allowed:
  _   __   __  ___   ___    _  __  ____ 
 | | / /  /  |/  /  / _ \  | |/_/ / __ \
 | |/ /  / /|_/ /  / , _/ _>  <  / /_/ /
 |___/  /_/  /_/  /_/|_| /_/|_|  \___\_\
 
blog/how_to_trap_interrupts_in_the_bash_shell.txt · Last modified: 2011-02-16 20:15 by brb