Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How: exit on error?

2 views
Skip to first unread message

J Krugman

unread,
May 30, 2004, 8:20:05 PM5/30/04
to

Is there a way to configure the running of a bash script so that
any error will cause the entire script to exit (with an appropriate
non-zero exit status)? I.e., to make something like this

#!/bin/bash
command1
command2 || command3
command4 ; command5 ; command6
# ...
commandN
exit 0

behave *as if* it were written like this

#!/bin/bash
command1 && \
( command2 || command3 ) && \
command4 && command5 && command6 && \
# ...
commandN && \
exit 0

without cluttering the code with all those '&&'s and backslashes?

TIA,

jill
--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

Barry Margolin

unread,
May 30, 2004, 8:29:12 PM5/30/04
to
In article <c9dtnl$j1q$1...@reader2.panix.com>,
J Krugman <jkrug...@yahbitoo.com> wrote:

> Is there a way to configure the running of a bash script so that
> any error will cause the entire script to exit (with an appropriate
> non-zero exit status)? I.e., to make something like this

set -e

From the man page:

-e Exit immediately if a simple command (see SHELL GRAMMAR
above) exits with a non-zero status. The shell does not
exit if the command that fails is part of an until or
while loop, part of an if statement, part of a && or ||
list, or if the command's return value is being inverted
via !. A trap on ERR, if set, is executed before the
shell exits.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Erik Max Francis

unread,
May 30, 2004, 8:29:37 PM5/30/04
to
J Krugman wrote:

> Is there a way to configure the running of a bash script so that
> any error will cause the entire script to exit (with an appropriate
> non-zero exit status)?

You can put ... || exit $? after each command.

Some shells, like zsh, have a facility for trapping on any command
failure, allowing you to do something whenever a command fails,
including exiting.

--
__ Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Life is a gamble so I should / Live life more carefully
-- TLC

J Krugman

unread,
May 30, 2004, 11:25:17 PM5/30/04
to

>In article <c9dtnl$j1q$1...@reader2.panix.com>,
> J Krugman <jkrug...@yahbitoo.com> wrote:

>> Is there a way to configure the running of a bash script so that
>> any error will cause the entire script to exit (with an appropriate
>> non-zero exit status)? I.e., to make something like this

>set -e

>From the man page:

> -e Exit immediately if a simple command (see SHELL GRAMMAR
> above) exits with a non-zero status. The shell does not
> exit if the command that fails is part of an until or
> while loop, part of an if statement, part of a && or ||
> list, or if the command's return value is being inverted
> via !. A trap on ERR, if set, is executed before the
> shell exits.

That's just the ticket! Many thanks!

0 new messages