Path: utzoo!utgpu!water!watmath!clyde!bellcore!faline!ulysses!allegra!princeton!udel!rochester!cornell!uw-beaver!teknowledge-vaxc!sri-unix!quintus!ok From: ok@quintus.UUCP Newsgroups: comp.unix.questions Subject: Re: exit(-1), 0 is sometimes magic Summary: but I tested it... Keywords: exit, zero, shells Message-ID: <571@cresswell.quintus.UUCP> Date: 24 Jan 88 04:48:49 GMT References: <502@cresswell.quintus.UUCP> <6935@brl-smoke.ARPA> <1179@wjvax.UUCP> <310@fig.bbn.com> Organization: Quintus Computer Systems, Mountain View, CA Lines: 55 Posted: Sat Jan 23 23:48:49 1988 In article <310@fig.bbn.com>, rsalz@bbn.com (Rich Salz) writes: > Nope. 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. That is, /bin/sh does > the "conceptually right thing" in that it takes a||b to mean, do b if a > failed. > > The Cshell takes a||b in the mathematical sense; do b if a does an exit(0): > /bin/csh -c "/bin/test -d foo || mkdir foo" > will never make the directory, or mkdir will spit if it already exists. > If you have a C-shell, do the following: cat >foo <<'EOF' #!/bin/csh (exit $1) && (echo true) || (echo false) 'EOF' chmod +x foo foo 0 prints true foo 1 prints false which indicates that the C-shell ***IS*** treating && and || the way I said it does. I tried Rich Salz's specific example, and it ***DID*** make the directory. This was under SunOS 3.2. The C-shell manual explicitly says that "Pipelines that are separated by && or || form conditional sequences in which the execution of pipelines on the right depends upon the success or failure, respectively, of the pipeline on the left." So it would be a very serious bug if Rich Salz were right. The C-shell, in addition to commands, has EXPRESSIONS, and in expressions && and || do what he thinks. But *ONLY* in expressions. Unlike the Bourne shell, the C-shell doesn't have you use commands in if and while. You use *expressions* there. The {command} form converts a command to an expression, but mapping exit 0 to 1 and exit non-zero to 0. (It's in the manual.) Suppose you want to create directory foo if it doesn't exist and the current directory is writable. Four ways: sh : test -d foo || ( test -w . && mkdir foo ) sh : if test ! -d foo && test -w . then mkdir foo fi csh: test -d foo || ( test -w . && mkdir foo ) csh: if ( ! { test -d foo } && { test -w . } ) mkdir foo ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ these are expressions! Yes, it is confusing that the same operators are used with opposite meanings. Even so, it is wise to try one's examples before claiming that they don't work...