Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting ó Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Advanced error handling in shell scripts

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 10-21-2009
script_man script_man is offline
Registered User
Join Date: Oct 2009
Last Activity: 17 November 2009, 11:10 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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>

In many cases the <handle error> 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.

    $1 # the command to be executed is passed in as an argument
    if [ $? -ne 0 ] ; then
      <handle error>

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:

  echo ""   >> $log_file
  echo "---------------------------------------------------------" >> $log_file
  echo "$1" >> $log_file
  $1        >> $log_file 2>&1
  if [ $return_code -ne 0 ] ; then
    echo "ERROR - $1 failed with $return_code"
    exit 1

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
Sponsored Links
Old Unix and Linux 10-21-2009
jlliagre jlliagre is offline Forum Advisor  
ɹǝsn sıɹɐlos
Join Date: Dec 2007
Last Activity: 21 October 2017, 12:39 PM EDT
Location: Outside Paris
Posts: 4,871
Thanks: 18
Thanked 635 Times in 554 Posts
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.
Sponsored Links
Old Unix and Linux 10-21-2009
steadyonabix steadyonabix is offline
Registered User
Join Date: Oct 2009
Last Activity: 1 January 2017, 3:21 PM EST
Location: UK
Posts: 330
Thanks: 14
Thanked 1 Time in 1 Post
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
Old Unix and Linux 10-21-2009
dr.house dr.house is offline
Registered User
Join Date: Dec 2008
Last Activity: 30 October 2011, 12:05 PM EDT
Location: Switzerland
Posts: 454
Thanks: 4
Thanked 27 Times in 27 Posts
My two bytes: Linux

if [ $? -ne 0 ]
  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)

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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Error handling in Unix shell scripting rksingh003 Shell Programming and Scripting 1 09-04-2009 10:55 AM
Error Checking in Shell scripts. dinjo_jo Shell Programming and Scripting 2 05-20-2009 08:03 AM
Handling Errors in Shell Scripts sarsani UNIX for Dummies Questions & Answers 2 12-24-2006 09:16 AM
Error Handling in Korn Shell scripts bhgopi UNIX for Advanced & Expert Users 4 09-06-2005 06:44 PM
Null handling in scripts mohanprabu Shell Programming and Scripting 3 01-20-2005 03:50 PM

All times are GMT -4. The time now is 01:41 PM.