csh if..else..if...endif...endif bug

lcc.bob%ucla-locus at sri-unix.UUCP lcc.bob%ucla-locus at sri-unix.UUCP
Sat Mar 3 03:47:30 AEST 1984


From:            Bob English <lcc.bob at ucla-locus>

The bug is simple: you can't (read CAN NOT) put an if...endif
inside the "else" clause in "csh".  White space has nothing to do
with it.  Other commands have nothing to do with it.  It simply
doesn't work correctly.  Try this script, for instance:

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

Even with a command before and after the nested "if", "shouldn't
get here" is printed.  In fact, you can put ANYTHING you want
between the else and the endif, csh will ignore it.  Fo example,
try:

#! /bin/csh
    set x=1
    if($x == 1) then
	    set y=2
    else
	    if
	    while
	    switch
	    foreach
	    echo hello there
	    if($y == 2) then
		    echo "line 1"
	    else
		    echo "line 2"
	    endif
	    echo "shouldn't get here"
    endif
    echo "finished"

I don't know about other flow-control constructions, but when
"csh" ignores shell commands because of an "else", it ignores
everything up to and including the next endif.  I don't
particularly like this feature, but that's the way it works.
Program around it or use a different shell.

--bob--

P.S. I don't particularly like "csh", but I do like job-control,
history, and aliasing, none of which are relevant to shell
programming, since "csh" can't seem to handle them when it's not
interactive.



More information about the Comp.unix.wizards mailing list