Advanced error handling in shell scripts

Hi all

  I've got a question regarding error handling in shell scripts. My background is mainly object oriented programming languages, but for a year or so I've been doing more and more \(bash\) shell scripting \(which I quite enjoy by the way\).

To handle errors in my scripts I often find myself doing something like:
<execute some command>
  if [ $? -ne 0 ] ;then
    <handle error>
  fi
  
 In many cases the &lt;handle error&gt; is the same for the commands executed, so you end up duplicating code. Thus I think it would be better to encapsulate this code in a function, e.g. 
exec_cmd()
  {
    $1 # the command to be executed is passed in as an argument
    if [ $? -ne 0 ] ; then
      <handle error>
    fi
  }       

And execute commands by calling this function:

exec_cmd "<some command>"
    Furthermore, the function can easily be enhanced to write the command as well as the command output and return code to a log file:
exec_cmd()
{
  echo ""   >> $log_file
  echo "---------------------------------------------------------" >> $log_file
  echo "$1" >> $log_file
  $1        >> $log_file 2>&1
  return_code=$?
  if [ $return_code -ne 0 ] ; then
    echo "ERROR - $1 failed with $return_code"
    exit 1
  fi
}

exec_cmd "<cmd1>"
exec_cmd "<cmd2>"
exec_cmd "<cmd2>"
...

I found this quite handy to handle errors in my scripts. If you write command, output and return code to a log file, it also makes it quite easy to investigate problems with scripts that run in the background.

Now my question is:
How is that as a practise? Is it common? Or would it be considerd as bad and if so why? Has the bash already got something like this build in? (E.g. there might be an exit on fail option; I also know that you can start scripts with an option which outputs the commands which are being executed).

How do the experienced people here do the error handling in their scripts?

Thanks and best wishes

I sometimes use a similar construction where I also add a question prompting the user to execute or not each command.

One limitation is handling structured instructions (i.e. loops, pipelines, conditional ) is more complex than single ones.

You don't always want to exit on error and even when you do you often want to output a meaningful error message so you should pass that in as well prior to testing the exit code.

I am not sure if your code is testing the return code of the command or the success of the redirection to the log, couldn't say without fooling around with it.

Probably better to tee the output rather than redirect it so you can see what is going on

My two bytes: :wink:

if [ $? -ne 0 ]
then
  takeNote "[error, long text]" # write error to log file (all systems)
  houseCall "[error, short text]" # send e-mail to admin (server only)
  popUp -info "[error, short text]" # pop up info window (client only)
fi

The according functions are stored in a separate file, to be sourced first thing by any script I'm using ...