Exiting on Error in a Shell Script

| No Comments

This is an embarrassing story. I had a bourne shell build script. In it I would perform various checkout and compilation steps, and finally copy the end result to a directory for deployment. It ran under Cygwin and when it worked, all was well.

When one of the build script steps did not succeed, the script kept running regardless. Unless I had happened to see an error message scroll by in the terminal window, I might miss a broken build. (I should mention that the build script takes a while to run and generates voluminous output.) I did miss broken builds. I need not elaborate on how painful this experience was.

The solution I ended up with is simple. The Cygwin shell is really bash, and bash has a nice error trapping feature.

  • changed #! /bin/sh to #! /bin/bash
  • added the following at the stop of my script
function error_exit {
 echo "exiting because of error"
 exit 1
}
trap error_exit ERR

Now I no longer need to babysit the build script. Three cheers for the Bourne Again Shell!

About this Entry

This page contains a single entry by Christian published on March 29, 2005 2:23 PM.

Laurent Bossavit's Incipient(thoughts): essay on was the previous entry in this blog.

Example of arraycopy() usage is the next entry in this blog.

Find recent content on the main index or look in the archive to find all content.