0

Possible Duplicate:
Error handling in BASH

I have one deployement script and i want to do exception handling in ubuntu shell script i.e. bash scripts. Is there any solution for this ?

Community
  • 1
  • 1
Hitul Mistry
  • 2,105
  • 4
  • 21
  • 29

1 Answers1

0

I'm not certain what you mean by exception handling, but if you are looking for try and catch (or something similar) you are out of luck. There is set -e, but I wouldn't say that was even close. The nearest I can think of is the ERR signal trap (ERR is a fake signal)

For example;

trap 'echo [$LINENO] Error: $?' ERR

In this case we only display the line number and the return code, but you could place any code inside the quotes, including a function call. The definition of a failure is a non-zero $?. It is not executed for conditionals or arithmetic structures, and is not carried into functions.

The trap can be "switched off" with:

trap – ERR

See also the DEBUG fake signal.

cdarke
  • 42,728
  • 8
  • 80
  • 84