bash / posix shells : chaining commands with || and &&



Maybe a little bit of a theoretic question, because it does not have
much practical value,
but maybe someone can give a quick answer:

Say I have 3 programs, a b and c. I would like to execute a, and iff
it fails (exit code not 0),
I also would like to execute b and c. I can think of 2 possibilities
how to do this:

# Alternative 1
a || (b;c)

# Alternative 2
if ! a
then
b
c
fi

Alternative 1 is compact, but involves a subshell. Alternative 2 is
more verbose, but involves
no subshell. Both do the job.

Now the question:

Is it possible to solve the problem using a compact representation, as
in alternative 1,
but without using a subshell? My guess is that the answer is "no", but
maybe somebody
could confirm this. Here is what I tried:

a || b ; c does not work, because || binds higher, and so c would be
always executed.

In my concrete case, I happen to know that "my" b will always return
exit code 0, so
I also tried

a || b && c

but here too, c is executed always, probably because || and && are
left-associative.
.



Relevant Pages

  • Re: bash / posix shells : chaining commands with || and &&
    ... I also would like to execute b and c. ... Alternative 1 is compact, but involves a subshell. ... more verbose, but involves ... Is it possible to solve the problem using a compact representation, ...
    (comp.unix.shell)
  • Re: bash / posix shells : chaining commands with || and &&
    ... RonaldOttoValentinFischer wrote: ... Is it possible to solve the problem using a compact representation, ... but without using a subshell? ... should execute the {... ...
    (comp.unix.shell)
  • Re: crontab and realplay
    ... you're running a shell command (via crontab) to execute a subshell ...
    (alt.os.linux)
  • Re: running ruby
    ... And there's me thinking that wouldn't work because it would execute in a ... subshell... ... I guess I should try these things before writing them off :-) ... Nobu Nakada ...
    (comp.lang.ruby)
  • Re: Problem with exit code
    ... I our bourne shell script that we execute on linux we have something ... When we call the compare_feature- function and read the line the exit ... subshell; using exit leaves the subshell, ...
    (comp.unix.shell)