On Tuesday 14 November 2006 07:16, Geoffrey wrote:
> Randall R Schulz wrote:
> > On Tuesday 14 November 2006 06:48, Geoffrey wrote:
> >> TheOldWiseKing wrote:
> >>> S A
> >>>
> >>> I found a good solution, here is the command:
> >>>
> >>> ls > file1.txt | cat file1.txt > file2.txt
> >>
> >> Not so wise OldWiseKing. :)
> >>
> >> I don't know what the pipe is for, but I don't think that will
> >> work. file2.txt will likely end up empty.
> >
> > No, it won't. When cat is given command-line file name arguments it
> > ignores the standard input.
>
> Yes, sometimes it will.  Do you think I posted without testing it
> myself?

What I said was true. As was the result you report.


> Results are unreliable. I tried this 10 times, twice file2.txt was
> empty, the other 8 it contained the same data as file1.txt.  Cut and
> paste from my latest attempt:

Had I stopped to think about it a moment longer, I'd have seen the 
problem.

The shell launches every command in a pipeline concurrently. That means 
the cat command in that pipeline can easily run before the ls completes 
and thus get thing from total ls output to partial output to none at 
all.


> rhws -> ls > file1.txt | cat file1.txt > file2.txt
> rhws -> ls -l file?.txt
> -rw-r--r--  1 esoteric esoteric 14168 Nov 14 10:11 file1.txt
> -rw-r--r--  1 esoteric esoteric 14168 Nov 14 10:11 file2.txt
>
> I'll not explain why this happens,

But I did, above.


> but it has to do with the improper 
> use of the pipe symbol.  If you research how the command line is
> parsed you will understand why the results is unrealiable.  The
> bottom line is, you don't want that solution, tee is your friend
> here.

It's not so much the parsing as it is the execution. And it's not 
an "improper" use, just an unuseful one.

But it's clear "tee" is the tool for the OP's purpose.


Randall Schulz
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to