On Mon, Mar 28, 2011 at 9:55 PM, givemecode <[email protected]> wrote:
> > > > Paul Smith-20 wrote: > > > > On Mon, 2011-03-28 at 11:15 -0700, givemecode wrote: > >> I modified a for loop in a makefile that looked like this: > >> > >> @for f in $(DRV_GEN_HEADER) ; do echo "#include \""$$f"\"" >> $@ ; > done > >> > >> which puts the output of echo into a new file I sepcified. > >> > >> changed to this: > >> > >> @for f in $(INCLUDE_DIR)/%.h); do \ > >> echo "#include \""$$f"\""; \ > >> done > '$@' > >> > >> which does the same thing but the writing is done after the loop > >> completes > >> (I think). Can someone please explain the difference and why it works? > > > > This is a question about shell programming, not about makefiles and is > > probably better asked in help list for shell hacking. > > > > However, I can answer it for you :-) > > > > Note that the latter does NOT do all the writing after the loop > > completes. The file is written to, one line at a time, just as in the > > first example. > > > > The first example says for each file, echo this one line and append it > > (hence ">>" which is append) to the file $@. > > > > The second example says, for each file echo this one line, and redirect > > all output from all commands run inside the for-loop to overwrite the > > file $@ (hence ">" which is overwrite--i.e., truncate and write from > > scratch). > > > > Some differences between the two: > > > > 1. If the file $@ exists before the command is invoked, then the > > first version will append the new content to the > > already-existing content... this is probably not what you want > > in this situation. The second version will always recreate the > > file from scratch. > > 2. If you wanted to print some content to stdout and some to the > > file, then you have to use the first version because the second > > version will put all stdout from all commands in the for loop > > into the file (well, you can play fancy tricks with file > > descriptors but it's hardly worth it). > > > > > > > > _______________________________________________ > > Help-make mailing list > > [email protected] > > http://lists.gnu.org/mailman/listinfo/help-make > > > > > > thanks to both of you. so the second version doesn't only write after the > for loop is complete? i guess that's just how i am reading it since it is > after the "done" keyword. what about this line tells the redirect to only > print the one line above it: "done > '$@'" ? > The 'done' keyword says that the LOOP is done, not output. A loop can, incidentally, be used as a scope for purposes of output redirection. These examples might help make it clear: stephan@tiny:~$ for i in a b c; do echo $i; done > foo stephan@tiny:~$ cat foo a b c stephan@tiny:~$ { for i in a b c; do echo $i; done > foo; echo bar; } > bar stephan@tiny:~$ cat foo a b c stephan@tiny:~$ cat bar bar But, as Paul said, this is really a Unix shell topic, and not Make. The exact rules of the shell constructs depend on the shell you've told make to use (defaullt=/bin/sh). -- ----- stephan beal http://wanderinghorse.net/home/stephan/ _______________________________________________ Help-make mailing list [email protected] http://lists.gnu.org/mailman/listinfo/help-make
