Re: nested loops: '; done ; done' vs. '; done done'



On Jun 2, 1:36 am, Frank Terbeck <f...@xxxxxxxxxxxxxxxxxxx> wrote:
Hello,

When nesting two for-loops, why does the following work?

for i in a b c ; do for e in x y z ; do echo $i$e ; done done

This is analogous to closing nested if's with fi fi fi ...

These keywords terminate the command, making it lexically clear that
the following material is the start of another command without the
need for a separating semicolon.

I was sure it had to be '; done ; done' at the end (which, of course,
works as well). But I tried the former in various shells (old-bourne,
ash, ksh93, zsh, bash...) and it always works. :-)

It's perfectly clear in the grammar given in the Single Unix
Specification.

The specific phrase rule which generates your above for_clause is:

for name linebreak in wordlist sequential_sep do_group

Thus the nested for is generated by the do_group, which is:

do compound_list done

The compound_list isn't required to generate a terminating semicolon.
A simple loop terminated by done counts, grammatically, as a compound
list. The precise derivation chain is:

compound_list
-> and_or
-> pipeline
-> pipe_sequence
-> command
-> compound_command
-> for_clause

And so you have ``do for_clause done'', which generates the done done.

That's a wrap! :)

.



Relevant Pages

  • FW: Use of FORCE
    ... "Consider using the FORCE command as a last resort when the CANCEL ... fails to terminate the address space within a reasonable time, ... FORCE with the ARM parameter. ...
    (bit.listserv.ibm-main)
  • "kill" group of commands from within script
    ... I have a problem with a small shell script. ... and then tries to terminate them ... When I run this command using KornShell, ...
    (comp.unix.shell)
  • Process.waitFor() returns, but threads reading output/error streams are blocked on read
    ... threads to read in the out/err streams, ... i'll include the java source below, but the problem seems extremely specific ... passed in on the command line). ... process is hung if i terminate the process the command line tool connects to ...
    (comp.lang.java.programmer)
  • Launch text command, how to wait for completion before continuing
    ... I'm trying to write a single scheduled task that updates Spybot Search ... window or the DOS command prompt. ... With arguments but no waiting for app to terminate: ... is a command that is internal to the intepreter. ...
    (microsoft.public.windowsxp.general)
  • Re: question about subprocess and shells
    ... When args finishes execution, does the shell terminate? ... shell will terminate as soon as it has started the command. ...
    (comp.lang.python)