[Top][All Lists]

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

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

From: Matthew Cengia
Subject: Re: [Help-bash] trap when expected return value may be non-zero?
Date: Thu, 2 May 2013 14:43:33 +1000
User-agent: Mutt/1.5.20 (2009-06-14)

On 2013-05-02 02:46, adrelanos wrote:
> 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...
> }

I'd do it like this:

| address@hidden:tmp$ trap "echo error" ERR INT TERM
| address@hidden:tmp$ false
| error
| address@hidden:tmp$ false || { ret=$?; true; }
| address@hidden:tmp$ echo $ret
| 1

So as you can see, false exits with status '1', and therefore the trap
is hit. However, if you OR the result with something that is guaranteed
to succeed (e.g. true or ':'), grabbing a copy of the return code of
the first command before calling 'true', you can avoid hitting the trap.

Matthew Cengia

Attachment: signature.asc
Description: Digital signature

reply via email to

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