Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!columbia!rutgers!caip!brl-adm!brl-smoke!smoke!lcc.richard@LOCUS.UCLA.EDU From: lcc.richard@LOCUS.UCLA.EDU (Richard Mathews) Newsgroups: net.unix-wizards Subject: Re: possible problem in csh if/then/endif Message-ID: <4431@brl-smoke.ARPA> Date: Wed, 8-Oct-86 08:29:00 EDT Article-I.D.: brl-smok.4431 Posted: Wed Oct 8 08:29:00 1986 Date-Received: Thu, 9-Oct-86 02:34:03 EDT Sender: news@brl-smoke.ARPA Lines: 42 > The following script ends up echoing "this should not happen" > > #! /bin/csh -fx > > set machine = string > set host = string > set foo = bar > > if ( $machine !~ $host ) then > if( $foo =~ bar ) then # no space after "if" > echo dummy if statement > endif > echo this should not happen > else > echo this should happen > endif The problem is in the functions search() and getword() in the C-shell. After finding "if ( false-expression ) then", the function search() looks for a matching else/endif. It uses getword() to look at each word until it finds either "else" or "endif". If it finds another "if", it increments a counter, and the search is not complete until the counter makes its way back to zero. So far, so good. The problem is that getword() considers "if(" to be a single word. This means that the counter does not get incremented when the "parser" sees this. The result is that the FIRST "endif" in your script terminates the FIRST "if" and the echo below that "endif" is executed. A separate bug results in an unexpected "else ... endif" being silently ignored. Try typing the following to a C-shell: else echo spam endif Richard M. Mathews Locus Computing Corporation lcc.richard@LOCUS.UCLA.EDU lcc.richard@UCLA-CS {ihnp4,trwrb}!lcc!richard {randvax,sdcrdcf,ucbvax,trwspp}!ucla-cs!lcc!richard