Re: OT: set -e in shell script

From: Manfred Lotz <manfred.lotz(at)arcor.de>
Date: Sat, 9 Aug 2014 20:34:18 +0200

On Sat, 9 Aug 2014 20:17:29 +0200
Patrick Hess <patrickhess(at)gmx.net> wrote:

> Manfred Lotz schrieb:
> > Wenn ich aber dieses habe
> >    test -f /not_existing  && true
> >
> > bricht das Script nicht ab, obwohl der && Ausdruch doch false
> > ergibt. Das verblüfft mich.
>
> Dieses Verhalten ist laut Manpage aber absolut korrekt:
>
> -e errexit
> Exit immediately if any untested command fails in
> non-interactive mode. The exit status of a command is considered to
> be explic- itly tested [...] if the command is the left hand operand
> of an `&&'' or ``||'' operator [...]
>

Sorry, ich hatte tatsächlich nicht nachgelesen.

> Kann mir auch kein Beispiel in der Praxis vorstellen, bei dem das
> von dir erwartete Verhalten sinnvoll wäre.
>

Aber ja doch. Ich will etwas ausführen, wenn ein Command gut geht.

Dafür kann man so was machen:

   test -f "/bla" && do_something "/bla"

Wenn ich nun ein Script umstelle mit
   set -e
   set -u
dann will ich ja, dass Fehler zum Abbruch führen. Ich muss nun mein
Coding anpassen, um Fehler abzufangen, die ich vorher nicht abgefangen
habe.

Der obige Fall führt nicht zum Abbruch, d.h. mein Coding sieht gut aus
ist es aber nicht und 'set -e' gibt keine Hilfestellung, damit ich
besser mache.

if [ -f "/bla" ]; then
   do_something "/bla"
else
   echo "handle error"
fi

-- 
Manfred
To Unsubscribe: send mail to majordomo(at)de.FreeBSD.org
with "unsubscribe de-bsd-questions" in the body of the message
Received on Sat 09 Aug 2014 - 20:34:27 CEST

search this site