possible problem in csh if/then/endif

Richard Mathews lcc.richard at LOCUS.UCLA.EDU
Wed Oct 8 22:29:00 AEST 1986


> The following script ends up echoing "this should not happen"
>
> #! /bin/csh -fx
>
> set machine = string
> set host    = string
> set foo     = bar
>
> if ( $machine !~ $host ) then
>	  if( $foo =~ bar ) then	# no space after "if"
>		  echo dummy if statement
>	  endif
>	  echo this should not happen
> else
>	  echo this should happen
> endif

The problem is in the functions search() and getword() in the C-shell.
After finding "if ( false-expression ) then", the function search() looks
for a matching else/endif.  It uses getword() to look at each word until
it finds either "else" or "endif".  If it finds another "if", it increments
a counter, and the search is not complete until the counter makes its way
back to zero.

So far, so good.  The problem is that getword() considers "if(" to be a
single word.  This means that the counter does not get incremented when
the "parser" sees this.  The result is that the FIRST "endif" in your
script terminates the FIRST "if" and the echo below that "endif" is
executed.

A separate bug results in an unexpected "else ... endif" being silently
ignored.  Try typing the following to a C-shell:

	else
	echo spam
	endif

Richard M. Mathews
Locus Computing Corporation		       lcc.richard at LOCUS.UCLA.EDU
					       lcc.richard at UCLA-CS
				 {ihnp4,trwrb}!lcc!richard
       {randvax,sdcrdcf,ucbvax,trwspp}!ucla-cs!lcc!richard



More information about the Comp.unix.wizards mailing list