weird if-then-else behavior in "csh"
Chris Torek
chris at mimsy.UUCP
Wed Jun 14 07:02:21 AEST 1989
In article <540 at acf2.NYU.EDU> mckenney at acf2.NYU.EDU (Alan Michael McKenney) writes:
>... The only difference between the [csh if/then/else example that works
>and the one that does not] is that the second "if" in the second file is
>immediately followed by "(", rather than " (". (In general, the "if"
>command seems to require spaces in many non-intuitive places.)
In general, the C shell's `parser' does not. (Parse, that is.)
> Is this a bug, a feature, or what?
How could it be anything but a bug? It is not documented. :-)
> And further, is there any good guide as to where spaces are required
>in "if" statements in "csh" (other than just putting them everywhere
>that they are legal)?
The trick is that, when skipping a false `if', the C shell uses a
completely different routine than when it is reading commands for
the purpose of actually executing them. The latter routine breaks
things at spaces, parentheses, and other `special' characters, while
the former breaks only at spaces. The false-if-skipping-code looks
for a line which, after being broken at spaces, starts with `if'
and ends with `then', with code like the following:
count = 1;
do {
read line;
break at spaces;
if (strcmp(word[0], "endif") == 0)
count--;
else if (strcmp(word[0], "if") == 0 &&
strcmp(word[nwords-1], "then") == 0)
count++;
} while (count);
So:
if (0) then # enter above pseudo-code
if then # count now reaches 2
if obviously&*csh[ would (never? like this` then # now 3
endif this is bad # now 2
endif # now 1
endif # count reaches 0
echo this gets echoed. # and we revert to the normal parser
But if you take out the first `if (0) then' you get an error (`if:
Expression syntax'); if you take out the first and second you get a
different error (`Unmatched `.'). If you put arguments after `endif'
inside a true `if' you get still another error.
The false-if-skipping-code recognises only `first word is "if",
last word is "then"', and breaks only at whitespace, so if you always
put a space after an `if' and one before a `then', you should never
have trouble.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris at mimsy.umd.edu Path: uunet!mimsy!chris
More information about the Comp.unix.questions
mailing list