[comp.unix.wizards] csh pleasures

tve@sprite.berkeley.edu (Thorsten von Eicken) (02/08/91)

Question: what does the following c-shell script print out?
Anybody care defending?

	#!/bin/csh -f
	set a=0
	set b=1
	if($a) then
		if($b) then
			echo one
		else
			echo two
		endif
	else
		echo three
	endif

Thorsten von Eicken - tve@sprite.berkeley.edu

tchrist@convex.COM (Tom Christiansen) (02/08/91)

From the keyboard of tve@sprite.berkeley.edu (Thorsten von Eicken):
:Question: what does the following c-shell script print out?
:Anybody care defending?
:
:	#!/bin/csh -f
:	set a=0
:	set b=1
:	if($a) then
:		if($b) then
:			echo one
:		else
:			echo two
:		endif
:	else
:		echo three
:	endif

On my system it prints out "three", because we fixed it.  On many
systems, it prints out "two".  The reason is that the csh misparses
"if(".  The only defense is for to stop people from writing ugly code
with parens next to their if's and while's.  More seriously, there
is no defense for the csh.  Just say no.  Trust me -- you'll be glad
you did so in the long run.

--tom
--
"Still waiting to read alt.fan.dan-bernstein using DBWM, Dan's own AI window 
manager, which argues with you for 10 weeks before resizing your window." 
### And now for the question of the month:  How do you spell relief?   Answer:
U=brnstnd@kramden.acf.nyu.edu; echo "/From: $U/h:j" >>~/News/KILL; expire -f $U

pfalstad@phoenix.Princeton.EDU (Paul Falstad) (02/08/91)

tve@sprite.berkeley.edu (Thorsten von Eicken) wrote:
>  if($a) then
>     if($b) then

This is a known bug.  To fix it you can put a space between the 'if' and
the '(' that starts the condition.  csh is too stupid to see the 'if'
if it is not followed by a space.  Use a real shell.  (sh, ksh, ... )

--
Paul Falstad, pfalstad@phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD
10 PRINT "PRINCETON CS"          | #include <std.disclaimer.h>
20 GOTO 10                       | [Your blood pressure just went up.]
Princeton University would like to apologize to everyone for this article.

rickert@mp.cs.niu.edu (Neil Rickert) (02/08/91)

In article <10880@pasteur.Berkeley.EDU> tve@sprite.berkeley.edu (Thorsten von Eicken) writes:
>Question: what does the following c-shell script print out?
>Anybody care defending?
>
>	#!/bin/csh -f
>	set a=0
>	set b=1
>	if($a) then
>...
 csh is rather fussy when it parses its input.  Try: if ( $a ) then 

In particular, "if(" is well known to cause problems.

-- 
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  Neil W. Rickert, Computer Science               <rickert@cs.niu.edu>
  Northern Illinois Univ.
  DeKalb, IL 60115                                   +1-815-753-6940

paul@sequoia.cray.com (Paul Dow (CRI-UK)) (02/08/91)

|> Anybody care defending?
|> 
|> 	#!/bin/csh -f
|> 	set a=0
|> 	set b=1
|> 	if($a) then
|> 		if($b) then
|> 			echo one
|> 		else
|> 			echo two
|> 		endif
|> 	else
|> 		echo three
|> 	endif
|> 


I'd like to know the background to this myself.  Over the years, slight differences between shells have caused me many headaches.

The above script will work "as expected" if a space is inserted between the
i and the "("s.  Some versions of csh will gripe if no space is provided.

Paul Dow.

p576spz@mpirbn.mpifr-bonn.mpg.de (S.Petra Zeidler) (02/09/91)

In article <1991Feb7.185547.12899@unicorn.cc.wwu.edu> n8743196@unicorn.cc.wwu.edu (Jeff Wandling) writes:
[lots of stuff deleted]
>It will print 'two'. Here's why: [...]
on a DECStation (Ultrix) it produces three. (with 'if(' )

Greetinx,
	spz
-- 
spz is spz@SpeckleC.MPIfR-Bonn.MPG.De. (S.Petra Zeidler)