[comp.unix.questions] is 'if' possible in /bin/sh when option -e is set ?

weinelt@sbsvax.cs.uni-sb.de (Bernhard Weinelt) (02/20/90)

Hello out there,

I have a problem using /bin/sh:

if I set the option -e  (stop execution if a command fails) I
can't use any if's, since writing

  if /bin/test [condition]
    then
    [commands1]
    else 
    [commands2]
  fi

never executes [commands2]. If the condition is true [commands1]
are executed, but if the condition is false, /bin/test returns
a value <> 0 and so the sh stops execution.

[Sorry, but test is not a builtin in /bin/sh on our VAX's running
ULTRIX and BSD4.3. In fact, in /bin/sh on our Suns, it is]

Any help?  Thanks in advance!

Bernhard
-- 
                                         ------------------------------------
                  __________________     | Bernhard Weinelt                 |
         (__)    /                  \    | Universitaet des Saarlandes      |
         (oo)   (    Many Greetings  )   | FB 10 - Informatik (Dept. of CS) |
  /-------\/  --'\__________________/    | Bau 36, Im Stadtwald 15          |
 / |     ||                              | D-6600 Saarbruecken 11, W-Germany|
*  ||----||                              | weinelt@cs.uni-sb.de             |
   ~~    ~~                              ------------------------------------

chris@mimsy.umd.edu (Chris Torek) (02/20/90)

In article <2903@sbsvax.cs.uni-sb.de> weinelt@sbsvax.cs.uni-sb.de
(Bernhard Weinelt) writes:
>if I set the option -e  (stop execution if a command fails) I
>can't use any if's ....

You have a copy of the Buggy Bourne Shell (tm), as distributed with
4.2 and 4.3 BSD and probably several other systems.  It can be identified
by running

	$ set -e
	$ if false; then echo gak; else echo ok; fi

and noting that the shell exits instead of printing `ok', and by

	$ set -e
	$ false || echo ok

which also exits (and should not), and by

	$ set -e
	$ while false; do :; done

To fix it, (1) get the source (2) change it in the obvious 3 places in
xec.c.  You will have to learn Bournegol.  Another alternative is to
replace /bin/sh with one of the free sh lookalikes, provided you can
find one that is enough alike.

As a work-around, you can `set +e' around all the tests that might fail.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris