(shell question) duplicate standard output?

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

(shell question) duplicate standard output?

Zhang Weiwu, Beijing
Hello. I already learnt that:

$ ls > result.txt
will redirect the output of ls(1) to result.txt

Now what if I wish to have the output of ls (1) redirect to two files a
the same time?

$ ls > result1.txt > result2.txt

Above command only redirect the result to result2.txt

Thanks.
--
锐业软服(国内业务)  http://www.realss.cn
Real SoftService      http://www.realss.com
销售咨询(Sales Department):    0086 592 20 99987 (Chinese, German,
English)
国际业务(International Sales): 0086 10 8460 6011 (German and English)
联系:厦门大学科技园,嘉庚二号楼6楼
邮政:厦门大学2312号信箱(邮编361005)

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

Dominique Leuenberger-2
 

>>> Reply on 14-11-2006 10:13:02 <<<> Hello. I already learnt that:
>
> $ ls > result.txt
> will redirect the output of ls(1) to result.txt
>
> Now what if I wish to have the output of ls (1) redirect to two files a
> the same time?
>
> $ ls > result1.txt > result2.txt
>
> Above command only redirect the result to result2.txt
>
Hi,
 
I think the command 'tee' might be an interesting case fir you.
 
Try
ls | tee result1.txt result2.txt
or, if you don't want screen output, you could do
ls | tee result1.txt > result2.txt
 
Regards,
Dominique
Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

TheOldWiseKing
In reply to this post by Zhang Weiwu, Beijing
S A

I found a good solution, here is the command:

ls > file1.txt | cat file1.txt > file2.txt

This will make two similar files containing the list of the current
directory.

Regards,...

TheOldWiseKing

??? wrote:

> Hello. I already learnt that:
>
> $ ls > result.txt
> will redirect the output of ls(1) to result.txt
>
> Now what if I wish to have the output of ls (1) redirect to two files a
> the same time?
>
> $ ls > result1.txt > result2.txt
>
> Above command only redirect the result to result2.txt
>
> Thanks.
>  
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

Darryl Gregorash
In reply to this post by Zhang Weiwu, Beijing
On 2006-11-14 01:59, 张韡武 wrote:

> Hello. I already learnt that:
>
> $ ls > result.txt
> will redirect the output of ls(1) to result.txt
>
> Now what if I wish to have the output of ls (1) redirect to two files a
> the same time?
>
> $ ls > result1.txt > result2.txt
>
> Above command only redirect the result to result2.txt
>
>  
info tee

Tee will redirect output to several files, as well as to stdout. If you
don't want output to stdout, do a "usual" redirection, eg:

ls | tee result1.txt result2.txt
This will send output to each file and to stdout.

ls | tee result1.txt > result2.txt
This will send output to result1.txt and redirect stdout to result2.txt.

OK, I gave you everything you were asking about -- but you should read
the info file anyway :-)

--


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

Prajjwal Devkota
In reply to this post by TheOldWiseKing
ls | tee test.txt > test1.txt

On Tue, 2006-11-14 at 10:23 +0200, TheOldWiseKing wrote:

> S A
>
> I found a good solution, here is the command:
>
> ls > file1.txt | cat file1.txt > file2.txt
>
> This will make two similar files containing the list of the current
> directory.
>
> Regards,...
>
> TheOldWiseKing
>
> ??? wrote:
> > Hello. I already learnt that:
> >
> > $ ls > result.txt
> > will redirect the output of ls(1) to result.txt
> >
> > Now what if I wish to have the output of ls (1) redirect to two files a
> > the same time?
> >
> > $ ls > result1.txt > result2.txt
> >
> > Above command only redirect the result to result2.txt
> >
> > Thanks.
> >  
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

Message Disclaimer: This electronic message may contain confidential information. If you have received it in error,
please immediately inform the sender and delete the mail and any attachments. Unless it relates to the official business
of UMN, any opinions, views and other information expressed in this document are those of the individual sender.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

Geoffrey-2
In reply to this post by TheOldWiseKing
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.

Maybe you meant:

ls > file1.txt; cat file1.txt > file2.txt

What you really want as someone else mentioned is a tee as in:

ls |tee file1.txt > file2.txt

It's a lot cleaner and probably more efficient.

--
Until later, Geoffrey

Those who would give up essential Liberty, to purchase a little
temporary Safety, deserve neither Liberty nor Safety.
  - Benjamin Franklin
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

Randall Schulz
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.

But this is a poor construction. Others have given the information the
OP needs, which is to learn about the "tee" command.


> ...


Randall Schulz
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

Geoffrey-2
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?

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:

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 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.

--
Until later, Geoffrey

Those who would give up essential Liberty, to purchase a little
temporary Safety, deserve neither Liberty nor Safety.
  - Benjamin Franklin
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

Randall Schulz
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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

TheOldWiseKing
In reply to this post by Geoffrey-2
Hi,

It sure works, and I tried it myself. the pipe operator | takes the
output of the certain command and Input it to the other command.

When saying ls > file1.txt, this will create a list of folders inside
the file1.txt
cat file1.txt will list the contents of file1.txt and take the output to
file2.txt

Regards,..

TheOldWiseKing

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.
>
> Maybe you meant:
>
> ls > file1.txt; cat file1.txt > file2.txt
>
> What you really want as someone else mentioned is a tee as in:
>
> ls |tee file1.txt > file2.txt
>
> It's a lot cleaner and probably more efficient.
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

Geoffrey-2
In reply to this post by Randall Schulz
Randall R Schulz wrote:

> 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.

No, you said "No, it won't", my results indicate it will.  See your own
words above.

>> 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.

Come on, there will be no output to standard out by 'ls > file1.txt' so
it's not proper usage.  It can produce unwanted results.  What more do
you need to say it's improper usage?  Argue for the sake of arguing?

--
Until later, Geoffrey

Those who would give up essential Liberty, to purchase a little
temporary Safety, deserve neither Liberty nor Safety.
  - Benjamin Franklin
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

Anders Johansson-3
In reply to this post by TheOldWiseKing
On Tuesday 14 November 2006 16:53, TheOldWiseKing wrote:
> Hi,
>
> It sure works, and I tried it myself. the pipe operator | takes the
> output of the certain command and Input it to the other command.
>
> When saying ls > file1.txt, this will create a list of folders inside
> the file1.txt
> cat file1.txt will list the contents of file1.txt and take the output to
> file2.txt

It might work and it might not, but either way it is an improper use of the
pipe. When you want to say "first run command1 then command 2" you should use

command1 && command2

Anders
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

Randall Schulz
In reply to this post by Geoffrey-2
On Tuesday 14 November 2006 07:59, Geoffrey wrote:

> ...
> >
> >> 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.
>
> Come on, there will be no output to standard out by 'ls > file1.txt'
> so it's not proper usage.

As used, the pipe in that command line is equivalent to an ampersand,
that's all.


> It can produce unwanted results.  What
> more do you need to say it's improper usage?

If it were "improper," the shell would tell you. Otherwise, like most
computer programs, it assumes you meant what you said, since it has
little recourse but to do so.


> Argue for the sake of arguing?

Well, clearly that's all you're doing. And you're doing it too
disrespectfully to even trim the quotations in the process.


RRS
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

Randall Schulz
In reply to this post by Anders Johansson-3
On Tuesday 14 November 2006 08:12, Anders Johansson wrote:

> On Tuesday 14 November 2006 16:53, TheOldWiseKing wrote:
> > Hi,
> >
> > It sure works, and I tried it myself. the pipe operator | takes the
> > output of the certain command and Input it to the other command.
> >
> > When saying ls > file1.txt, this will create a list of folders
> > inside the file1.txt
> > cat file1.txt will list the contents of file1.txt and take the
> > output to file2.txt
>
> It might work and it might not, but either way it is an improper use
> of the pipe. When you want to say "first run command1 then command 2"
> you should use
>
> command1 && command2

Just to be clear (and I know you know this, Anders, but others may not),
the semicolon is unconditional sequencing regardless of how the first
command exits or terminates, the second command is executed. The
double-ampersand make the execution of the second command contingent
upon normal termination and a successful exit status (0) from the first
command.


> Anders


Randall Schulz
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

Geoffrey-2
In reply to this post by TheOldWiseKing
TheOldWiseKing wrote:
> Hi,
>
> It sure works, and I tried it myself. the pipe operator | takes the
> output of the certain command and Input it to the other command.

You don't understand how the whole command line is handled by the shell.

> When saying ls > file1.txt, this will create a list of folders inside
> the file1.txt
> cat file1.txt will list the contents of file1.txt and take the output to
> file2.txt

Okay, I've been doing the UNIX thing since the mid 80s.  YOU NEED TO
LISTEN TO THE FOLKS WHO ARE ANSWERING YOUR QUESTION.  Your solution will
not work reliably.

So, listen to folks who have been there and done that or learn the hard
way.  It's your choice.

--
Until later, Geoffrey

Those who would give up essential Liberty, to purchase a little
temporary Safety, deserve neither Liberty nor Safety.
  - Benjamin Franklin
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: (shell question) duplicate standard output?

Geoffrey-2
In reply to this post by Randall Schulz
Randall R Schulz wrote:

> On Tuesday 14 November 2006 07:59, Geoffrey wrote:
>> ...
>>>> 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.
>> Come on, there will be no output to standard out by 'ls > file1.txt'
>> so it's not proper usage.
>
> As used, the pipe in that command line is equivalent to an ampersand,
> that's all.

For the sake of the original poster, HE DOES NOT REALIZE THAT.  So why
lead him down such a path?  Teach people to use the tools right.

>> It can produce unwanted results.  What
>> more do you need to say it's improper usage?
>
> If it were "improper," the shell would tell you. Otherwise, like most
> computer programs, it assumes you meant what you said, since it has
> little recourse but to do so.

The pipe does not serve the purpose the original poster intended.  You
are simply clouding the issue by continuing the discourse.  Further, the
proper intention of the pipe is to accept input from a process.  The
leading process is not sending any input.

>> Argue for the sake of arguing?
>
> Well, clearly that's all you're doing. And you're doing it too
> disrespectfully to even trim the quotations in the process.

Brother.  Deflect the issue to something totally unrelated.

--
Until later, Geoffrey

Those who would give up essential Liberty, to purchase a little
temporary Safety, deserve neither Liberty nor Safety.
  - Benjamin Franklin
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]