[comp.unix.questions] csh, exit, &&, ||

chris@mimsy.UUCP (Chris Torek) (01/24/88)

(I overrode Rich's followup-to header, since there are buggy csh's out
there.)
In article <310@fig.bbn.com> rsalz@bbn.com (Rich Salz) writes:
>The use of && and || in csh is the opposite of that in /bin/sh.
>	/bin/sh -c "/bin/test -d foo || mkdir foo"
>Means if the foo directory doesn't exist, make it. ...
>	/bin/csh -c "/bin/test -d foo || mkdir foo"
>will never make the directory, or mkdir will spit if it already exists.

BUZZ!

Sorry, contestant number 3.

:-)

Old versions of the C shell did indeed have this bug.  It has been
fixed for years.  If your C shell does this, complain to your vendor
(or upgrade from 3BSD!).  I believe the documentation has always
claimed that the C shell's `&&' and `||' constructs work in the
same way as the Bourne shell's.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

dhesi@bsu-cs.UUCP (Rahul Dhesi) (01/24/88)

In article <10318@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
[saying that && and || in csh now work as advertised]

However, I've never been able to make the following work:

     cd /xyz || echo "/xyz doesn't seem to exist"

Both csh and sh are broken.
-- 
Rahul Dhesi         UUCP:  <backbones>!{iuvax,pur-ee,uunet}!bsu-cs!dhesi

ok@quintus.UUCP (Richard A. O'Keefe) (01/25/88)

> In article <310@fig.bbn.com> rsalz@bbn.com (Rich Salz) writes:
> >The use of && and || in csh is the opposite of that in /bin/sh.
> >	/bin/sh -c "/bin/test -d foo || mkdir foo"
> >Means if the foo directory doesn't exist, make it. ...
> >	/bin/csh -c "/bin/test -d foo || mkdir foo"
> >will never make the directory, or mkdir will spit if it already exists.

In every release of SunOS I've used, this does actually work.
The problem is that the C-shell has TWO operators spelled "&&"
and TWO operators spelled "||".

The C-shell distinguishes between commands and expressions.
	command_1 && command_2
means execute command_1, and if it exits ZERO, execute command_2.
	command_1 || command_2
means execute command_1, and if it exits NON-ZERO, execute command_2.

But in *expressions*, && and || have the same meaning as in C.
Which is the direct opposite of their interpretation with commands!
Which is just one of the reasons why I refuse to use the C-shell for
writing scripts.

The moral of this for C is that doing this sort of thing to operators
is more trouble than it's worth.  For example, the &&& and |||
operators which were proposed recently would not be a good idea.

dhesi@bsu-cs.UUCP (Rahul Dhesi) (01/26/88)

In article <574@cresswell.quintus.UUCP> ok@quintus.UUCP (Richard A. O'Keefe) 
writes:
>But in *expressions*, && and || have the same meaning as in C.
>Which is the direct opposite of their interpretation with commands!

Actually, no.  The C-shell uniformly gives the following meanings
to these operators.

     &&     evaluate the next item iff the previous one succeeded
     ||     evaluate the next item iff the previous one failed

The meaning of "item" is defined by the grammar of expressions or
pipelines.  That success is represented by zero for processes and by
nonzero for expressions are UNIX and C conventions, not specifically a
csh convention.
-- 
Rahul Dhesi         UUCP:  <backbones>!{iuvax,pur-ee,uunet}!bsu-cs!dhesi