Wednesday, April 17, 2013

Bash Trap Control+C


Do you want to catch control-c keyboard interrupts in your Bash program? Use the Bash builtin trap command to catch system signals. The following runs control_c() when a user interrupts the main() section with a Control-C (SIGINT)


#!/bin/bash

cleanup()
# example cleanup function
{
  rm -f /tmp/tempfile
  return $?
}

control_c()
# run if user hits control-c
{
  echo -en "\n*** Ouch! Exiting ***\n"
  cleanup
  exit $?
}

# trap keyboard interrupt (control-c)
trap control_c SIGINT

# main() loop
while true; do read x; done


found at: http://hacktux.com/bash/control/c

No comments:

Post a Comment

Cybermap

Internet Storm Center Infocon Status

Internet Storm Center Infocon Status
Internet Storm Center Infocon Status