csh problem involving nested ifs?

Chris Torek chris at mimsy.UUCP
Tue Jul 11 03:30:38 AEST 1989


In article <685 at wuphys.UUCP> marty at wuphys.UUCP (Marty Olevitch) writes:
>It appears that csh does not correctly handle nested if-then-else
>statements.

This is false.  It does handle them, but it is finicky.

>Am I missing some bug in this script?

[all but the `if' lines deleted]

>if($v1 == 1) then
>	if($v2 == 1) then
>	if($v2 == 1) then

You must put a space after the `if', before the `('; and you must have
a space between the `)' and the `then' (which must be the last word on
the line, after stripping comments).

The reason is that the C shell's `skip to end of false construct' code
uses a completely different line parser than usual.  The `execute this line'
parser breaks lines at whitespace and at the characters ( ) & | and
perhaps a few others.  (&& and || are collected as a unit before causing
word breaks.)  The `skip to end of false if' routine winds up doing

	if (strcmp("if(", "if") == 0 && strcmp("then", "then") == 0)

in the particular cases given above.  Had you written

	if ($v2 == 1)then

it would have tried

	if (strcmp("if", "if") == 0 && strcmp(")then", "then") == 0)

and also failed.  Only

	if ($v2 == 1) then

and similar constructs pass its test.  (One such construct is

	if (this, {which is not &syntactically! correct, goes undetected then
		the c shell sucks
	endif

which counts as an `if-then' during false-if skipping, but causes an
error otherwise.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix.wizards mailing list