csh question; a possible explanation

daemon at decwrl.UUCP daemon at decwrl.UUCP
Sat Mar 3 06:11:55 AEST 1984


From: logic::puder  (Karl Puder	DTN 225-4979)
wivax!evans message of 29-Feb-1984 09:07:50, Re: re: csh question
indicates that adding a space after the ifs below (s|if(|if (|g) will
prevent the incorrect behavior.  My (possibly correct) explanation follows.


    #! /bin/csh				{
    set x=1				    x=1;
    if($x == 1) then			    if(x == 1){
	    set y=2			        y=2;
    else				    }else{
	    if($y == 2) then		        if(y == 2){
		    echo "line 1"		    printf("line 1");
	    else			        }else{
		    echo "line 2"		    printf("line 2");
	    endif			        }
	    echo "shouldn't get here"	    printf("shouldn't get here");
    endif				    }
    echo "finished"			printf("finished");
					}

What happens is this: the first 'if' finds a true condition, and so "set
y=2" is executed. Then the 'else' tells csh to "skip statements until you
see an endif". The problem is that the "skip until endif" code does not
notice the second 'if' unless it is followed by a space, so the wrong
'endif' gets matched.

Why didn't Bill Joy use Lex and/or Yacc?!! One beauty of UNIX is that there
are all these great tools for writing programs. USE THEM!

Karl Puder	DEC AITG	(617)568-4979	...!decwrl!rhea!logic!puder



More information about the Comp.unix.wizards mailing list