[net.unix-wizards] csh question

scw%ucla-locus@cepu.UUCP (02/25/84)

From:  Steve Woods <cepu!scw@ucla-locus>


	I'm having trouble with nesting of csh "if-else-endif". In the following

    ----------------------
    #! /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");
					}
    ----------------------

    when you run it, you get the "shouldn't get here" message printed out.
    Does anyone know why? 

Because you should, look at the ~equivalent 'c' program. the thing is that
the endif is much like a closing brace '}'.

reece%nadc@sri-unix.UUCP (02/27/84)

Steve,
Thanks for taking the trouble to answer. My example didn't convey
what I wanted to do--that is to nest an if-then-else in the if
or else part of another if-then-else. I don't think you can do that.
Jim

mcneal@fortune.UUCP (02/28/84)

#R:sri-arpa:-1694400:fortune:11600069:000:1001
fortune!mcneal    Feb 28 10:14:00 1984



	I'm having trouble with nesting of csh "if-else-endif". In the following

    ----------------------
    #! /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");
					}
    ----------------------

You problem is due to the fact that only one 'endif' is needed.  The 
documentation on the csh states:

	if (expr) then
		....
	else if (expr2) then
		....
	else
		....
	endif

	Any number of else-if pairs are possible; only one endif is needed.


	Richard McNeal
UUCP:	{sri-unix,amd70,hpda,harpo,ihnp4,allegra}!fortune!mcneal
DDD:	(415)595-8444
USPS:	Fortune Systems Corp, 101 Twin Dolphins Drive, Redwood City, CA 94065

fair@dual.UUCP (02/29/84)

[RAID]
	I'm having trouble with nesting of csh "if-else-endif". In the following

    ----------------------
    #! /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");
					}
    ----------------------

You problem is due to the fact that only one 'endif' is needed.  The 
documentation on the csh states:

	if (expr) then
		....
	else if (expr2) then
		....
	else
		....
	endif

	Any number of else-if pairs are possible; only one endif is needed.


	Richard McNeal
UUCP:	{sri-unix,amd70,hpda,harpo,ihnp4,allegra}!fortune!mcneal
DDD:	(415)595-8444
USPS:	Fortune Systems Corp, 101 Twin Dolphins Drive, Redwood City, CA 94065
----

It should also be noted that csh is particular about spacing, so there should
be a space between the `if' and the first paren. I believe there was even a
bug associated with nested if's in Csh reported some time back...

	Erik E. Fair

	dual!fair@BERKELEY.ARPA
	{ihnp4,ucbvax,cbosgd,decwrl,amd70,fortune,zehntel}!dual!fair
	Dual Systems Corporation, Berkeley, California

evans@wivax.UUCP (Barry Evans) (02/29/84)

Someone posted an article stating they had a problem with the following
csh script, and someone else posted a followup stating there
was an incorrect number of endif's.  Not so.  Removing one of the
endif's is a hack, which just happens to work.  I'm convinced that 
there is a bug in Csh, and that the logic in the script is correct.
Reason being, if you include a whitespace after the if's, then the
following script works as it is supposed to.  I haven't seen anything in the
documentation saying that you must include  white-spaces though.  Also,
in expressions, you must include spaces before and after the
conditions, which the documentation doesn't mention either, but for these
you will get error messages.

    #! /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");
					}

-barry
-- 
Barry Evans   {apollo, cadmus, decvax, linus}!wivax!evans
              Wang Institute (617) 649-9731 x383

chris@umcp-cs.UUCP (02/29/84)

That problem:

	if(falsecond)then
		stuff
		if(cond)then
			otherstuff
		endif
		morestuff
	endif

is caused by C-shell braindamage.  Inside a false if, "if(thing)then"
is not recognized as a multi-line if.

Solution: either use "if (cond) then" with spaces around the ()s, or,
use the Bourne shell.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris.umcp-cs@CSNet-Relay

eric@burdvax.UUCP (Eric Marshall) (01/21/86)

	Why can't csh use stdio? Looking at the csh code, csh has its
own printf routine which prints using putchar, instead of putc, which
is what stdio uses. What's the difference between the two? The infamous
doprnt routine is also different. The makefile for csh contains
a comment that an old doprnt needs to be used whose output we
can trap. What does that mean? Whats this all about?

Thanks in advance.


Eric Marshall
System Development Corporation, a Burroughs Company
P.O. Box 517
Paoli, PA. 19301
(215) 648-7223

USENET: sdcrdcf!burdvax!eric
        {sjuvax,ihnp4,akgua,cadre}psuvax1!burdvax!eric
ARPANET: PAYTON@BBNG