Tuesday, October 16, 2012

Bash programming tips - part 4

In part 1, 2 and 3 we have defined script outline, set up configuration and defined helper functions. Now will discuss error handling. It is amazing that almost none of scripts I have seen have none. It is really simple, just look at code snippet bellow.

#catch script return value

  #check if commands executed successfully
  if [ $_ERROR -ne 0 ]
    debug "ERROR" "We have an error. Handle it. !!!!"

With _ERROR=$? we have stored the exit code of the last command executed to variable _ERROR. In Linux world all commands return 0 on success or positive integer on failure.

No comments: