[comp.unix.questions] How is && supposed to work in csh?

BACON@MTUS5.BITNET (Jeffery Bacon) (10/21/89)

(This is also a test to see whether this new netnews server I just put in
blows up or not.)

I have the following chunk of code on a Sun 3 running SOS 4.0.3...

if ( -f /bin/sun4 ) /bin/sun4 && set arch=sun4 && goto gotarch
if ( -f /bin/sun4c ) /bin/sun4c && set arch=sun4 && goto gotarch
if ( -f /bin/sun3 ) /bin/sun3 && set arch=sun3 && goto gotarch
if ( -f /bin/sun3x ) /bin/sun3x && set arch=sun3 && goto gotarch
gotarch:

The idea here is to implement a cheap 'arch' cmd inside my script instead of
having to start another shell (by running 'arch'). But, on SOS 4.0.3 for
68020's, there is no /bin/sun4c. So of course that if comes back false.
What I would expect is that the shell would go on to the next line (i.e.
go look for /bin/sun3). Instead, it only skips the /bin/sun4c cmd and goes
ahead and executes the rest of the line! Is this normal? I thought that
if you use an 'if () cmd' construct, it would take the rest of the line...?
Or is it treating the '&&' as the end-of-line?

Would someone be kind enough to explain? I've RTFM'ed and RTF(C-Shell Field
Guide)'ed; neither were very explicit, and there are no gurus to ask because
I'm supposedly one of them. (We're not unix-literate here.)

Thanks.
-------
Jeffery Bacon
Computing Technology Svcs., Michigan Technological University
bitnet: bacon@mtus5  uucp (alternate): <backbone>!rutgers!clip!anet!bacos

ok@cs.mu.oz.au (Richard O'Keefe) (10/21/89)

In article <89293.143521BACON@MTUS5.BITNET>, BACON@MTUS5.BITNET (Jeffery Bacon) writes:
> if ( -f /bin/sun4 ) /bin/sun4 && set arch=sun4 && goto gotarch
> if ( -f /bin/sun4c ) /bin/sun4c && set arch=sun4 && goto gotarch
> if ( -f /bin/sun3 ) /bin/sun3 && set arch=sun3 && goto gotarch
> if ( -f /bin/sun3x ) /bin/sun3x && set arch=sun3 && goto gotarch
> gotarch:

The csh(1) manual page says about
	if (expr) command
that "command must be a simple command, not a pipeline, a command
list, or a parenthesized command list."  One of the possible cases
for a pipeline is a sequence of commands separated by "&&".


A simpler scheme would be to do
	if      (-f /bin/sun4  && { /bin/sun4  }) then
	    set arch=sun4
	else if (-f /bin/sun4c && { /bin/sun4c }) then
	    set arch=sun4
	else if (-f /bin/sun3  && { /bin/sun3  }) then
	    set arch=sun3
	else if (-f /bin/sun3c && { /bin/sun3c }) then
	    set arch=sun3
	else
	    echo "Unable to determine architecture type"
	    exit 1
	endif

I find that it is generally better to write scripts using the Bourne shell
(sh) than the C shell (csh):  the sh language has rather fewer gotchas.  It
is also easier to do some things.  For example, I would really like the
error message to come out on stderr rather than stdout.  In sh it's easy:
	echo "Unable to determine architecture type" >&2
Given that you are going to try running up to four programs /bin/sun{3,4}{,c}
anyway, it seems odd to boggle at using the /bin/arch script to do it.

BACON@MTUS5.BITNET (Jeffery Bacon) (10/22/89)

I feel suitably humbled now, thanks.
It would be nice if there was a better way than having to use /bin/sun{*}
in the first place, but since that's the provided mechanism...bleah.

(It's also nice to know that netnews is working. Thanks for responding!)