Read this: I am really keen on sharing this knowledge with you in whatever format is most useful for you.
Click Here to buy the full 85-page PDF for only £4.99, $9.99 or €6.99, or the Kindle eBook or Paperback (UK here - more countries and details here)

Have you come to find out about ShellShock? Click here

Trap

Trap is a simple, but very useful utility. If your script creates temporary files, such as this simple script which replaces FOO for BAR in all files in the current directory, /tmp is clean when the script exits. If it gets interrupted partway through, though, there could be a file lying around in /tmp:
#!/bin/sh

trap cleanup 1 2 3 6

cleanup()
{
  echo "Caught Signal ... cleaning up."
  rm -rf /tmp/temp_*.$$
  echo "Done cleanup ... quitting."
  exit 1
}

### main script
for i in *
do
  sed s/FOO/BAR/g $i > /tmp/temp_${i}.$$ && mv /tmp/temp_${i}.$$ $i
done
The trap statement tells the script to run cleanup() on signals 1, 2, 3 or 6. The most common one (CTRL-C) is signal 2 (SIGINT). This can also be used for quite interesting purposes:
#!/bin/sh

trap 'increment' 2

increment()
{
  echo "Caught SIGINT ..."
  X=`expr ${X} + 500`
  if [ "${X}" -gt "2000" ]
  then
    echo "Okay, I'll quit ..."
    exit 1
  fi
}

### main script
X=0
while :
do
  echo "X=$X"
  X=`expr ${X} + 1`
  sleep 1
done
The above script is quite fun - it catches a CTRL-C, doesn't exit, but just changes how it's running. How this could be useful for positive and negative effect is left as an exercise to the reader:) This particular example concedes to quit after 4 interrupts (or 2000 seconds). Note that anything will be killed by a kill -9 <PID> without getting the chance to process it.

Here is a table of some of the common interrupts:

NumberSIGMeaning
00On exit from shell
1SIGHUPClean tidyup
2SIGINTInterrupt
3SIGQUITQuit
6SIGABRTAbort
9SIGKILLDie Now (cannot be trap'ped)
14SIGALRMAlarm Clock
15SIGTERMTerminate

Note that if your script was started in an environment which itself was ignoring signals (for example, under nohup control), the script will also ignore those signals.

Read this: I am really keen on sharing this knowledge with you in whatever format is most useful for you.
Click Here to buy the full 85-page PDF for only £4.99, $9.99 or €6.99, or the Kindle eBook or Paperback (UK here - more countries and details here)

Have you come to find out about ShellShock? Click here
Steve's Bourne / Bash shell scripting tutorial
Share on Twitter Share on Facebook Share on LinkedIn Share on Identi.ca Share on StumbleUpon

Get the Book:

Buy my 600-page Shell Scripting Book...


Or from other Amazon in other countries: USA, Germany, France, Spain, Italy, Canada, Japan, India, Brazil
Or from other retailers

Buy this Tutorial in Paperback or for Kindle:


USA, UK, Germany, France, Spain, Italy
Or Kindle in these countries: Canada, Australia, Japan, India, Brazil, Mexico

Buy this Tutorial as a DRM-Free PDF

(Free Sample)

$9.99 US Dollars

£4.99 UK Pounds

€6.99 Euros

You can join our Shell Scripting community on Facebook:

DefectiveByDesign.org