bash / posix shells : chaining commands with || and &&
- From: RonaldOttoValentinFischer <ro.naldfi.scher@xxxxxxxxx>
- Date: Mon, 23 Jun 2008 03:42:19 -0700 (PDT)
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.
.
- Follow-Ups:
- Re: bash / posix shells : chaining commands with || and &&
- From: Stephane CHAZELAS
- Re: bash / posix shells : chaining commands with || and &&
- From: Dave B
- Re: bash / posix shells : chaining commands with || and &&
- Prev by Date: Re: Editing Crontab Entries
- Next by Date: Re: bash / posix shells : chaining commands with || and &&
- Previous by thread: Editing Crontab Entries
- Next by thread: Re: bash / posix shells : chaining commands with || and &&
- Index(es):
Relevant Pages
|