[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Error handling question
From: |
Ciprian Dorin, Craciun |
Subject: |
Re: Error handling question |
Date: |
Mon, 9 Nov 2009 08:38:56 +0200 |
On Mon, Nov 9, 2009 at 8:23 AM, Ciprian Dorin, Craciun
<ciprian.craciun@gmail.com> wrote:
> On Mon, Nov 9, 2009 at 4:49 AM, Chet Ramey <chet.ramey@case.edu> wrote:
>> Ciprian Dorin, Craciun wrote:
>>> Shouldn't any of the following scripts print `error`? (Bash
>>> 4.0.35(2)-release on ArchLinux.)
>>>
>>> Or I've miss-interpreted the documentation...
>>>
>>> Thanks,
>>> Ciprian.
>>>
>>>
>>> ~~~~
>>> set -e -o pipefail
>>> ( false ; echo ok ; ) || echo error
>>> ~~~~
>>>
>>> ~~~~
>>> set -e -o pipefail
>>> ( false ; echo ok ; ) | true || echo error
>>> ~~~~
>>>
>>> ~~~~
>>> set -e -o pipefail
>>> { false ; echo ok ; } || echo error
>>> ~~~~
>>>
>>> ~~~~
>>> set -e -o pipefail
>>> { false ; echo ok ; } | true || echo error
>>> ~~~~
>>
>> No. Since `set -e' has no effect on the left side of the || or &&
>> operators, all of the commands preceding the || exit with status 0.
>>
>> Chet
>> --
>> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>> ``Ars longa, vita brevis'' - Hippocrates
>> Chet Ramey, ITS, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/
>
>
> Sorry, but I don't understand at all... So please bare with me and
> make me understand.
>
> So I've interpreted `set -e` as a way to tell bash to treat any
> process exiting with non-zero (and not succeeded by a || ), as an
> error and end the current shell / sub-shell.
>
> Thus if I say: `set -e ; { false ; true ; }` it works, but when I
> put the `||`, it doesn't...
>
> So my question is how can I solve this problem? (And obtain the
> needed behaviour.) (I see `()` and `{}` as blocks in normal
> programming languages (of course with some particularities), and
> non-zero exit codes as exceptions. And this is very helpful to write
> robust bash scripts.)
>
> Thanks,
> Ciprian.
I'm also pasting from the documentation.
The interesting part is :`The shell does not exit if the command
that fails is part of the command list immediately following [...]
part of any command executed in a && or |⎪ list, [...], The
option applies to [...] each sub-shell environment [...] and may cause
subshells to exit before executing all the commands in the subshell`.
So from this I understand that the (parent) shell doesn't exit if
the (child) sub-shell fails and is at left of `||`. But that the
subshell inherits the `-e` option, and should exit.
Ciprian.
~~~~
-e Exit immediately if a pipeline (which may
consist of a single simple command),
a subshell command enclosed in parentheses, or
one of the commands exe‐
cuted as part of a command list enclosed by
braces (see SHELL GRAMMAR above)
exits with a non-zero status. The shell does
not exit if the command that
fails is part of the command list immediately
following a while or until keyword, part
of the test following the if or elif reserved
words, part of any
command executed in a && or ⎪⎪ list except
the command following the final
&& or ⎪⎪, any command in a pipeline but the
last, or if the command's return
value is being inverted with !. A trap on ERR,
if set, is executed before the shell exits.
This option applies to the shell environment
and each sub‐
shell environment separately (see COMMAND
EXECUTION ENVIRONMENT above),
and may cause subshells to exit before executing
all the commands in the subshell.
~~~~
- Error handling question, Ciprian Dorin, Craciun, 2009/11/08
- Re: Error handling question, Chet Ramey, 2009/11/08
- Message not available
- Re: Error handling question, Jan Schampera, 2009/11/09
- Re: Error handling question, Ciprian Dorin, Craciun, 2009/11/09
- Re: Error handling question, Greg Wooledge, 2009/11/09
- Re: Error handling question, Ciprian Dorin, Craciun, 2009/11/09
- Re: Error handling question, Greg Wooledge, 2009/11/09
- Re: Error handling question, Sven Mascheck, 2009/11/09
- Re: Error handling question, Chet Ramey, 2009/11/09
- Re: Error handling question, Sven Mascheck, 2009/11/09