From: Jack Bates on
How can I make my script exit immediately if "svn export" in the
following exits with a non-zero status?

#!/bin/sh

# Exit immediately if a simple command exits with a non-zero status
set -e

[...]

# Export from vendor repository
REV=`svn export http://svn.symfony-project.com/branches/1.4 $TMPDIR/
symfony | sed -n '$s/^Exported revision \([[:digit:]]\+\)\.$/\1/p'`

[...]

Here's the complete (short) script, http://qubit-toolkit.googlecode.com/svn/symfony/cron.daily/drop

We use it nightly to pull upstream code into our repository. It should
exit immediately if a command exits with a non-zero status - however
last night "svn export" failed, the script didn't exit, and
consequently the upstream code was removed from our repository

When the script calls "svn export", it needs,

1) to get the "exported revision" from standard output
2) to exit immediately if the exit code is non-zero

The script exits immediately if most commands exit with a non-zero
status, because it calls "set -e"

I guess that "set -e" doesn't apply in the case of "svn export"
because it's executed in a subshell?

How can I get the "exported revision" from standard output, and exit
immediately if the exit code is non-zero?
From: Jack Bates on
On Jul 26, 1:39 pm, "Chris F.A. Johnson" <cfajohn...(a)gmail.com> wrote:
> On 2010-07-26, Jack Bates wrote:
> > How can I make my script exit immediately if "svn export" in the
> > following exits with a non-zero status?
>
> > #!/bin/sh
>
> > # Exit immediately if a simple command exits with a non-zero status
> > set -e
>
> > [...]
>
> > # Export from vendor repository
> > REV=`svn exporthttp://svn.symfony-project.com/branches/1.4$TMPDIR/
> > symfony | sed -n '$s/^Exported revision \([[:digit:]]\+\)\.$/\1/p'`
>
> > [...]
>
> > Here's the complete (short) script,http://qubit-toolkit.googlecode.com/svn/symfony/cron.daily/drop
>
> > We use it nightly to pull upstream code into our repository. It should
> > exit immediately if a command exits with a non-zero status - however
> > last night "svn export" failed, the script didn't exit, and
> > consequently the upstream code was removed from our repository
>
> > When the script calls "svn export", it needs,
>
> > 1) to get the "exported revision" from standard output
> > 2) to exit immediately if the exit code is non-zero
>
> > The script exits immediately if most commands exit with a non-zero
> > status, because it calls "set -e"
>
> > I guess that "set -e" doesn't apply in the case of "svn export"
> > because it's executed in a subshell?
>
> > How can I get the "exported revision" from standard output, and exit
> > immediately if the exit code is non-zero?
>
>    Test the command itself:
>
> REV=`svn exporthttp://svn.symfony-project.com/branches/1.4$TMPDIR/symfony |
>   sed -n '$s/^Exported revision \([[:digit:]]\+\)\.$/\1/p'` || exit 1

Thank you very much Chris, but this isn't working - the script still
doesn't exit immediately

Here's a simplified example,


#!/bin/sh

set -e

FOO=`false | sed s/foo/bar/g` || exit 1

echo didnt exit


^ This example should exit immediately after the call to "false" - it
shouldn't execute the last line, "echo didn't exit"

However when I execute it, it prints "didn't exit",


$ ./test
didnt exit
$


I also tried "FOO=`false || exit 1 | sed s/foo/bar/g` || exit 1" but
it still doesn't exit without executing the last line

How can I get this script to exit immediately if "svn export" exits
with a non-zero status?