Richard Lightman richard at
Mon Jan 15 06:53:41 PST 2001

Misquoted from Jack Detrick on 2001/01/14 at 21:42 +0000:

> You can do error-checking in a script.  I have 4 separate scripts that
> run (at last count) to install my LFS system all the way up to the
> kernel "make menuconfig" and the reboot, and in each and every one of
> those I have error checking every step of the way.  Even if something so
> small as a file or two being copied for /mnt/lfs/bin to /mnt/lfs/usr/bin
> or whatever screws up, the install stops and tells me.  It's all about
> exit codes.  Here's my simple error-checking function:

That is far too much work. Use bash's "set -e" to exit on any error
that is not infront of a && or ||. Split your script into many little
ones. Bash frequently reports errors as being at the end of the file,
when they are really in the middle. This way you know which file
cause the problem. Generate a log for each package, and delete
the log for packages that installed without errors.

For the less puzzled: have a master script that can continue with
other non-dependant packages even if an earlier one failed. Set
it up so that you do not have to repeat compiles that worked even
if there is a bug in the master script. (outline below)

For the downright peverse: Have the master script compile multiple
packages simultaneously on different machines (I am working on it

Create a new directory, and put demo in it.
../demo setup   creates the other files you will need.
../demo run     tells you which package succeded and failed
   look at the log for the first package that failed: log_killer
   fix the bug: remove line 3 of killer
../demo run     killer will run, so will depends because
               it depended on killer completeing
   fix the bug in false (delete line 3)
../demo run     it worked this time!
../demo setup   get the errors back
  Change the tail in demo to tall
../demo run     Look at that crash
   Put an x on the end of the first line of this file
../demo run     Now you know where the error is

#!/bin/bash -e
# Bash script error handling demonstration: demo

worked() { echo $1 worked; rm log_$1; echo $1 >>worked;}
failed() { echo $1 failed; echo $1 >>failed;}
run()    { (set -x; . ./$1) >log_$1 2>&1 && worked $1 || failed $1;}

case $1 in
    while [ -s todo ]; do
      run $(head -1 todo)
      tail +2 todo >todo~
      mv todo~ todo
    mv failed todo
    > failed
    ! [ -s todo ] && echo All complete || echo Fix some bugs

  setup) # create faulty scripts to compile bogus packages:
    for package in true killer depends false; do
      echo 'echo normal output'             >$package
      echo 'echo error output >&2'         >>$package
      case "$package" in
        killer)  echo 'killer)'            >>$package;;
        depends) echo 'grep killer worked' >>$package;;
        *)       echo $package             >>$package;;
      echo 'echo final output'             >>$package

      echo $package >>todo # add package to todo list
      rm -f log_$package   # Ensure clean run

List of bash builtins:            help
Help on a bash built in like set: help set
everything else, man and info are your friends

Unsubscribe: send email to lfs-discuss-request at
and put unsubscribe in the subject header of the message

More information about the lfs-dev mailing list