Advanced error handling in shell scripts | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Advanced error handling in shell scripts

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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:


Code:
<execute some command>
  if [ $? Ėne 0 ] ;then
    <handle error>
  fi

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.


Code:
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:

Code:
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:

Code:
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
Sponsored Links
    #2  
Old 10-21-2009
jlliagre jlliagre is offline Forum Advisor  
ɹǝsn sıɹɐlos
 
Join Date: Dec 2007
Last Activity: 30 October 2014, 11:42 PM EDT
Location: Paris
Posts: 4,434
Thanks: 16
Thanked 474 Times in 427 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
    #3  
Old 10-21-2009
steadyonabix steadyonabix is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 13 July 2014, 5:51 AM EDT
Location: UK
Posts: 323
Thanks: 12
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
    #4  
Old 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:


Code:
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 ...
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 06:39 AM.