Exception-handling in Shell programs

Hi all,

I am writing a set of scripts and some of these are nested within others. I want to be able to execute a single high-level script at the command-line, which in turn may invoke other scripts as required, and when a failure occurs in any of the nested scripts, I want to be able to track the deepest-nested command causing the failure and preferably a call trace.

To do this, trap could be used I was told but I am not sure trap's functionality is enough for my need. What do you think is the best way here?

It's a constraint at this place that I use only shell and not a full-blown programming language. Any help from you guys is greatly appreciated.

Best Regards,
Chaitanya

Will each of the scripts that you invoke be doing detailed error checking? If the last script executed runs a command that fails, it could print the error and return a failure code.

If you write each script to do that, your trace takes care of itself, without any additional effort required (other than what you need to put in for the scripts themselves).

Hi blowtorch,

No, I wasn't planning on putting detailed error checks in each of the scripts...they are too long (5000+ lines) and doing that that would take me forever. I am looking more from a less-involved solution. Do you think it's possible actually? Something like - (I am vague I know but it's the best I can do!) -

  1. Start a guarded environment at the beginning of each script (guarded as in - each command is executed and if it succeeds, alright; otherwise, break the whole program right there).

  2. Execute each command in the script (without explicitly coding for error-checking, i.e.)

  3. Run an event like trap in case of failure, and fire an email.

What do you say...?