help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] trap when expected return value may be non-zero?


From: adrelanos
Subject: [Help-bash] trap when expected return value may be non-zero?
Date: Thu, 02 May 2013 02:46:10 +0000

Hello!

I have a global trap, but in one function I am expecting a non-zero
return code. Example:

trap "error_handler" ERR INT TERM

examplefunct() {
   # code...

   service someservice status
   returncode="$?"

   # more code...
}

When service return a non-zero value, the error_handler is called.

So I have to work around it. Example:

trap "error_handler" ERR INT TERM

examplefunct() {
   # code...

   trap "" ERR INT TERM
   service someservice status
   local returncode="$?"
   trap "error_handler" ERR INT TERM

   # more code...
}

Well, it works, but thats not so pretty.

Basically, I just want to the trap being called in case something really
unexpected happens. Most times I use traps as a way to find bugs in my
scripts. In this case, for example if "service" wouldn't exist (not in
$PATH or whatever) or if "someservice" wouldn't exist..

Is there a way to combine this? Let trap catch syntax and other errors
while accepting non-zero return values?

Cheers!
adrelanos



reply via email to

[Prev in Thread] Current Thread [Next in Thread]