weird if-then-else behavior in "csh"

Alan Michael McKenney mckenney at acf2.NYU.EDU
Wed Jun 14 01:15:57 AEST 1989


Subject: Wierd "csh" behavior with nested if-then-else

[ Note: please reply to mckenney at acf8.nyu.edu -- I am posting from a ]
[                                ^^^^                                ]
[ different machine because of software problems on acf8.            ]


    The following behaves as expected, if executed by "csh" using
the "source" command:
------------------------------------------------------------------------
if ( 1 == 2 ) then
	echo "first if clause (begin) (shouldn't print)"
	if ( 3 == 2 ) then
		echo "second if clause (shouldn't print)"
	else
		echo "else of second if clause (shouldn't print)"
	endif
	echo "first if clause (end) (shouldn't print)"
else
	echo "else of first if clause (should print)"
	if ( 1 == 1 ) then
		echo  "third if clause (should print)"
	endif
endif
------------------------------------------------------------------------

    The following doesn't:
------------------------------------------------------------------------
if ( 1 == 2 ) then
	echo "first if clause (begin) (shouldn't print)"
	if( 3 == 2 ) then
		echo "second if clause (shouldn't print)"
	else
		echo "else of second if clause (shouldn't print)"
	endif
	echo "first if clause (end) (shouldn't print)"
else
	echo "else of first if clause (should print)"
	if ( 1 == 1 ) then
		echo  "third if clause (should print)"
	endif
endif
------------------------------------------------------------------------
the lines marked "should print" aren't, while the 3rd and 4th that say
"shouldn't print" are printed.  The only difference between the two is
that the second "if" in the second file is immediately followed by
"(", rather than " (".  (In general, the "if" command seems to require
spaces in many non-intuitive places.)

    I have observed this behavior on a VAX running 4.3BSD, a Sun-4
running SunOS-3.2 (I think), and a Sun-3 running "Sun UNIX 4.2 Release
3.5".

    Is this a bug, a feature, or what?

    And further, is there any good guide as to where spaces are required
in "if" statements in "csh" (other than just putting them everywhere
that they are legal)?


Alan McKenney        E-mail:  mckenney at acf8.nyu.edu          (INTERNET)
Courant Institute,NYU         ...!cmcl2!acf8!mckenney        (UUCP)
-- 
Alan McKenney        E-mail:  mckenney at acf8.nyu.edu          (INTERNET)
Courant Institute,NYU         ...!uunet!cmcl2!acf8!mckenney  (UUCP)



More information about the Comp.unix.questions mailing list