Skip to contentSkip to conversation

dash, ash for loop oddity

DISCUSSION Maximilian Bernöcker 2 participants 2 messages Sep 21, 2008 – Feb 22, 2009

lore
Share & tools
lore
#0 lore raw

Hi,

according to the grammar
<http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_10_02>

| for_clause       : For name linebreak                            do_group
|                  | For name linebreak in          sequential_sep do_group
|                  | For name linebreak in wordlist sequential_sep do_group
| [...]
| linebreak        : newline_list
|                  | /* empty */

the following script should be legal:

#####
#!/bin/dash
for i
in a b c
do
echo $i
done
#####

but it fails with:

| ./a.sh: 3: Syntax error: "in" unexpected (expecting "do").

bash, zsh and NetBSDs ksh are doing fine while NetBSDs sh, based on ash fails, too.

Max

#1 lore raw
Quoted 5 lines · Maximilian Bernöcker

Hi,

according to the grammar
<http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_10_02>

Quoted 8 lines
| for_clause       : For name linebreak                            do_group
|                  | For name linebreak in          sequential_sep do_group
|                  | For name linebreak in wordlist sequential_sep do_group
| [...]
| linebreak        : newline_list
|                  | /* empty */

the following script should be legal:

Thanks. Fixed with this patch.

commit 22e8fb41be2bfd0e6757c80d6365af9080a3b4a1

Author: Herbert Xu <[email protected]>
Date:   Sun Feb 22 19:47:52 2009 +0800

[PARSER] Allow newlines after var name in for statements

POSIX allows newlines before the "in" keyword in for statements so we should too.

Thanks to Maximilian Bernöcker for reporting this.

ChangeLog +1 -0
index dcb7182..d7e0f79 100644
@@ -3,6 +3,7 @@
 	* Fix dowait signal race.
 	* Remove EXSIG.
 	* Do not close stderr when /dev/tty fails to open.
+	* Allow newlines after var name in for statements.
 
 2009-01-14  Herbert Xu <[email protected]>
 
src/parser.c +2 -2
index ab66f30..425c457 100644
@@ -357,7 +357,7 @@ TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
 		n1 = (union node *)stalloc(sizeof (struct nfor));
 		n1->type = NFOR;
 		n1->nfor.var = wordtext;
-		checkkwd = CHKKWD | CHKALIAS;
+		checkkwd = CHKNL | CHKKWD | CHKALIAS;
 		if (readtoken() == TIN) {
 			app = &ap;
 			while (readtoken() == TWORD) {
@@ -383,7 +383,7 @@ TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
 			 * Newline or semicolon here is optional (but note
 			 * that the original Bourne shell only allowed NL).
 			 */
-			if (lasttoken != TNL && lasttoken != TSEMI)
+			if (lasttoken != TSEMI)
 				tokpushback++;
 		}
 		checkkwd = CHKNL | CHKKWD | CHKALIAS;

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt