nested if's in csh

moss%brl-vld at sri-unix.UUCP moss%brl-vld at sri-unix.UUCP
Wed Feb 29 23:36:24 AEST 1984


From:      Gary S Moss ~Software Development Team~ <moss at 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.



More information about the Comp.unix.wizards mailing list