help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: What is the difference between > and >> in a makefile.


From: Paul Smith
Subject: Re: What is the difference between > and >> in a makefile.
Date: Mon, 28 Mar 2011 18:11:40 -0400

On Mon, 2011-03-28 at 13:17 -0700, givemecode wrote:
> Stephan Beal-2 wrote:
> > 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
> 
> That is what's confusing: the redirect is AFTER the loop is done, so how
> does ALL the echos get written to a file instead of the last one ? thanks!

You're getting confused because you're being too literal.

The redirect is not "after the loop is done".  The redirect is
redirecting the _output of the (commands in the) loop_.

Just like if you run:

        grep foo /some/big/file > /tmp/foo.out

the output from the grep command is being written incrementally
to /tmp/foo.out, one line at a time (by default stdout is line buffered)
as it is printed from grep.  It's not all saved up somewhere, then
dumped into /tmp/foo.out after grep is completely done.

In the same way, the for-loop is considered by the shell to be one
"command" (although it runs inside the shell not in a sub-process, like
grep), and the output of that "command" (that is everything that happens
in the body of the loop as it runs) is written to the output file.

You can think of this:

        for v in a b c; do echo $v; done > /tmp/v.out

like this, if it helps:

        { for v in a b c; do echo $v; done; } > /tmp/v.out

but you don't need the {}'s because the shell knows the entire loop
(between the do ... done) is a single "command".

You can do very cool things like this:

        grep foo /some/big/file \
                | sed 's/xxy/zzq/' \
                | while read foo bar; do
                        some -crazy -thing "$foo" "$bar"
                  done \
                > result.file

etc.  sh is neat!




reply via email to

[Prev in Thread] Current Thread [Next in Thread]