* Stop script if one command returns != 0
@ 2014-05-14 14:01 Florian Lindner
2014-05-14 14:19 ` Chris Johnson
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Florian Lindner @ 2014-05-14 14:01 UTC (permalink / raw)
To: zsh-users
Hello,
I have a script that I source with a number of commands and exports. Is
there a way to tell zsh (or even in a portable way to sh) to stop
execution of this script if any of the commands returns a return code
other than zero?
Thanks,
Florian
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Stop script if one command returns != 0
2014-05-14 14:01 Stop script if one command returns != 0 Florian Lindner
@ 2014-05-14 14:19 ` Chris Johnson
2014-05-14 14:20 ` Peter Stephenson
2014-05-14 14:21 ` Florian Lindner
2 siblings, 0 replies; 9+ messages in thread
From: Chris Johnson @ 2014-05-14 14:19 UTC (permalink / raw)
To: Florian Lindner; +Cc: zsh-users
Florian Lindner sent me the following 0.2K:
> I have a script that I source with a number of commands and exports.
> Is there a way to tell zsh (or even in a portable way to sh) to stop
> execution of this script if any of the commands returns a return
> code other than zero?
Setting the ERR_EXIT option will cause the script to bail:
setopt ERR_EXIT
ls "file does not exist"
echo "this won't appear"
--
Chris Johnson
johnch@uwec.edu
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Stop script if one command returns != 0
2014-05-14 14:01 Stop script if one command returns != 0 Florian Lindner
2014-05-14 14:19 ` Chris Johnson
@ 2014-05-14 14:20 ` Peter Stephenson
2014-05-14 14:31 ` Florian Lindner
2014-05-14 16:22 ` Roman Neuhauser
2014-05-14 14:21 ` Florian Lindner
2 siblings, 2 replies; 9+ messages in thread
From: Peter Stephenson @ 2014-05-14 14:20 UTC (permalink / raw)
Cc: zsh-users
On Wed, 14 May 2014 16:01:19 +0200
Florian Lindner <mailinglists@xgm.de> wrote:
> I have a script that I source with a number of commands and exports. Is
> there a way to tell zsh (or even in a portable way to sh) to stop
> execution of this script if any of the commands returns a return code
> other than zero?
(Count the responses. I'm guessing 3 to 5...)
set -e
is standard across Bourne-like shells including zsh --- this is
equivalent to the zsh option "ERREXIT".
pws
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Stop script if one command returns != 0
2014-05-14 14:20 ` Peter Stephenson
@ 2014-05-14 14:31 ` Florian Lindner
2014-05-14 15:10 ` Bart Schaefer
2014-05-14 16:22 ` Roman Neuhauser
1 sibling, 1 reply; 9+ messages in thread
From: Florian Lindner @ 2014-05-14 14:31 UTC (permalink / raw)
To: Peter Stephenson; +Cc: zsh-users
Am 14.05.2014 16:20, schrieb Peter Stephenson:
> On Wed, 14 May 2014 16:01:19 +0200
> Florian Lindner <mailinglists@xgm.de> wrote:
>> I have a script that I source with a number of commands and exports.
>> Is
>> there a way to tell zsh (or even in a portable way to sh) to stop
>> execution of this script if any of the commands returns a return code
>> other than zero?
>
> (Count the responses. I'm guessing 3 to 5...)
:-P
> set -e
>
> is standard across Bourne-like shells including zsh --- this is
> equivalent to the zsh option "ERREXIT".
Problem is that set -e or ERREXIT kill my shell when called in a sourced
script.
Regards,
Florian
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Stop script if one command returns != 0
2014-05-14 14:31 ` Florian Lindner
@ 2014-05-14 15:10 ` Bart Schaefer
0 siblings, 0 replies; 9+ messages in thread
From: Bart Schaefer @ 2014-05-14 15:10 UTC (permalink / raw)
To: zsh-users
On May 14, 4:31pm, Florian Lindner wrote:
}
} > (Count the responses. I'm guessing 3 to 5...)
}
} :-P
Wrong time of day in the USA for that guess, Peter ...
} > set -e
} >
} > is standard across Bourne-like shells including zsh --- this is
} > equivalent to the zsh option "ERREXIT".
}
} Problem is that set -e or ERREXIT kill my shell when called in a sourced
} script.
This should work:
trap 'return $?' ZERR
Or if you want to get fancier:
[[ -o interactive ]] && trap 'return $?' ZERR || set -e
To remove the trap automatically (very recent zsh versions only):
trap '(){ trap - ZERR; return $1 } $?' ZERR
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Stop script if one command returns != 0
2014-05-14 14:20 ` Peter Stephenson
2014-05-14 14:31 ` Florian Lindner
@ 2014-05-14 16:22 ` Roman Neuhauser
1 sibling, 0 replies; 9+ messages in thread
From: Roman Neuhauser @ 2014-05-14 16:22 UTC (permalink / raw)
To: Peter Stephenson; +Cc: zsh-users
# p.stephenson@samsung.com / 2014-05-14 15:20:11 +0100:
> On Wed, 14 May 2014 16:01:19 +0200
> Florian Lindner <mailinglists@xgm.de> wrote:
> > I have a script that I source with a number of commands and exports. Is
> > there a way to tell zsh (or even in a portable way to sh) to stop
> > execution of this script if any of the commands returns a return code
> > other than zero?
>
> (Count the responses. I'm guessing 3 to 5...)
>
> set -e
>
> is standard across Bourne-like shells including zsh --- this is
> equivalent to the zsh option "ERREXIT".
errexit as specified by SUS/POSIX is mostly useless and very dangerous.
http://pubs.opengroup.org/onlinepubs/9699919799/:
% The -e setting shall be ignored when executing the compound list
% following the while, until, if, or elif reserved word, a pipeline
% beginning with the ! reserved word, or any command of an AND-OR list
% other than the last.
you might think this means that
if { f; g; h}; then ... fi
will execute all of f, g, h no matter if any of them fails.
that's true, but not the whole truth: errexit won't be in effect
*globally* while the condition is getting executed. this goes down
to functions:
---------- 8< ----------
#!/bin/sh
set -e
f()
{
# h must not run when g fails;
# we have errexit, hooray!
do-this
do-that
}
if f; then # f is executed with set +e in effect !!!
...
fi
f && echo ok # again, f runs with set +e
---------- >8 ----------
http://austingroupbugs.net/view.php?id=537
--
roman
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Stop script if one command returns != 0
2014-05-14 14:01 Stop script if one command returns != 0 Florian Lindner
2014-05-14 14:19 ` Chris Johnson
2014-05-14 14:20 ` Peter Stephenson
@ 2014-05-14 14:21 ` Florian Lindner
2014-05-14 14:35 ` Peter Stephenson
2 siblings, 1 reply; 9+ messages in thread
From: Florian Lindner @ 2014-05-14 14:21 UTC (permalink / raw)
To: zsh-users
Am 14.05.2014 16:01, schrieb Florian Lindner:
> Hello,
>
> I have a script that I source with a number of commands and exports.
> Is there a way to tell zsh (or even in a portable way to sh) to stop
> execution of this script if any of the commands returns a return code
> other than zero?
Ok, just a second after the mail I learned about set -e. The problem is
when sourcing the script it kills my entire shell.
I tried installing a trap
trap 'echo "Hallo"' TERM INT ABRT EXIT
(just a as first shot, trying to catch almost anything) but changed
nothing.
Any ideas how do deal with that?
Thanks!
Florian
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Stop script if one command returns != 0
2014-05-14 14:21 ` Florian Lindner
@ 2014-05-14 14:35 ` Peter Stephenson
2014-05-14 14:49 ` Peter Stephenson
0 siblings, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 2014-05-14 14:35 UTC (permalink / raw)
To: zsh-users
On Wed, 14 May 2014 16:21:55 +0200
Florian Lindner <mailinglists@xgm.de> wrote:
> Ok, just a second after the mail I learned about set -e. The problem is
> when sourcing the script it kills my entire shell.
>
> I tried installing a trap
>
> trap 'echo "Hallo"' TERM INT ABRT EXIT
>
> (just a as first shot, trying to catch almost anything) but changed
> nothing.
>
> Any ideas how do deal with that?
Wrap your sourced file in a function and use the zsh-specific option
ERR_RETURN.
source_with_err_return() {
setopt localoptions errreturn
source "$@"
}
Test:
% >script
print Got here
false
print But not here
% source_with_err_return ./script
Got here
& for the paranoid:
% fn2() {
function> source_with_err_return ./script
function> false
function> print Got here OK as ERR_RETURN is no longer on.
function> }
% fn2
Got here
Got here OK as ERR_RETURN is no longer on.
pws
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Stop script if one command returns != 0
2014-05-14 14:35 ` Peter Stephenson
@ 2014-05-14 14:49 ` Peter Stephenson
0 siblings, 0 replies; 9+ messages in thread
From: Peter Stephenson @ 2014-05-14 14:49 UTC (permalink / raw)
To: zsh-users
On Wed, 14 May 2014 15:35:44 +0100
Peter Stephenson <p.stephenson@samsung.com> wrote:
> Wrap your sourced file in a function and use the zsh-specific option
> ERR_RETURN.
>
> source_with_err_return() {
> setopt localoptions errreturn
> source "$@"
> }
(I haven't got the reposted mail I'm replying to yet, so you might see
this first...)
It turns out I was accidentally or otherwise smart enough to make
err_return apply to sourced files, so if you don't want function scope
you can get away with...
{
setopt err_return
source ./script
} always {
unsetopt err_return
}
pws
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2014-05-14 16:31 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-14 14:01 Stop script if one command returns != 0 Florian Lindner
2014-05-14 14:19 ` Chris Johnson
2014-05-14 14:20 ` Peter Stephenson
2014-05-14 14:31 ` Florian Lindner
2014-05-14 15:10 ` Bart Schaefer
2014-05-14 16:22 ` Roman Neuhauser
2014-05-14 14:21 ` Florian Lindner
2014-05-14 14:35 ` Peter Stephenson
2014-05-14 14:49 ` Peter Stephenson
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).