[net.unix-wizards] nested if's in csh

eric%cit-vax@sri-unix.UUCP (02/28/84)

From:  Eric Holstege <eric@cit-vax>

The following shell script, when run with the four permutations of inputs
t 0 0
t 1 0
t 0 1
t 1 1
works correctly in all cases.

#! /bin/csh -f
set a = $1
set b = $2
if ( $a ) then
    if ( $b ) then
	echo a and b
    else
	echo a and not b
    endif
else
    if ( $b ) then
	echo not a, but b
    else
	echo neither a nor b
    endif
endif

However, if spaces are left out between the "if" and the "(", it no
longer works correctly. Thus the space appears to be the key. This seems
to be a bug.

		    * Eric Holstege  Caltech, Pasadena, CA.
		    * eric@cit-vax

moss%brl-vld@sri-unix.UUCP (02/29/84)

From:      Gary S Moss ~Software Development Team~ <moss@brl-vld>

> The following shell script, when run with the four permutations of inputs
> t 0 0
> t 1 0
> t 0 1
> t 1 1
> works correctly in all cases.
>
> #! /bin/csh -f
> set a = $1
> set b = $2
> if ( $a ) then
>    if ( $b ) then
>	echo a and b
>    else
>	echo a and not b
>    endif
> else
>    if ( $b ) then
> 	echo not a, but b
>    else
> 	echo neither a nor b
>    endif
> endif
>
> However, if spaces are left out between the "if" and the "(", it no
> longer works correctly. Thus the space appears to be the key. This seems
> to be a bug.

This is not the same situation as Jim presented.   Jim's problem stems
from having a nested 'if' in the 'else' branch of the outside conditional
such that an 'else' is followed immediately by the nested 'if'.
Your example has such a situation :

	else
	    if ( $b ) then ...
	
HOWEVER, even though the bug in the csh syntax makes this ambiguous,
the two possible expressions are functionally equivalent, the alternate
is below.