|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi
How can I redirect stout and sterror in a file and only sterror inanother file at the same time? For example, If I run the script: /script.sh >> log.txt 2>&1 It will write in "log.txt" stout and sterror. But, If I want write stout and sterror in "log.txt" and at the sametime sterror in "error.log", it's possible? Thank you. -- I'm using an evaluation license of nemo since 203 days. You should really try it! http://www.malcom-mac.com/nemo |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 13 Bøe, 15:13, luciosan <lucio...@gmail.com> wrote:
> Hi > > How can I redirect stout and sterror in a file and only sterror > inanother file at the same time? > > For example, If I run the script: > /script.sh >> log.txt 2>&1 > > It will write in "log.txt" stout and sterror. > > But, If I want write stout and sterror in "log.txt" and at the > sametime sterror in "error.log", it's possible? > > Thank you. > > -- > I'm using an evaluation license of nemo since 203 days. > You should really try it!http://www.malcom-mac.com/nemo Well, this is not a good solution, but it is a solution of your problem I think... ./script.sh 2>&1 >log.txt | ./two_files_logger "log.txt" "error.log" where two_files_logger is script like this: #!/bin/bash while read line ; do echo $line >> $1 echo $line >> $2 done I am pretty sure that there are many better, faster and "all-in-one- script" solutions but that is what i would do... |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Mar 13, 7:13 pm, luciosan <lucio...@gmail.com> wrote:
> Hi > > How can I redirect stout and sterror in a file and only sterror > inanother file at the same time? > > For example, If I run the script: > /script.sh >> log.txt 2>&1 > > It will write in "log.txt" stout and sterror. > > But, If I want write stout and sterror in "log.txt" and at the > sametime sterror in "error.log", it's possible? > > Thank you. > > -- > I'm using an evaluation license of nemo since 203 days. > You should really try it!http://www.malcom-mac.com/nemo This should work for you: ../script.sh 2>err | tee out >> err Now the file "err" should have both stdout and stderr. Meantime, the file out should contain your stdout only. For example, if script.sh contains the following lines: rm non_existent_file echo hello and you execute it as above, err should contain both error message from rm and a hello, while out will only contain the "hello" message |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
2008-03-13, 14:13(+00), luciosan:
[...] > How can I redirect stout and sterror in a file and only sterror > inanother file at the same time? > > For example, If I run the script: > /script.sh >> log.txt 2>&1 > > It will write in "log.txt" stout and sterror. > > But, If I want write stout and sterror in "log.txt" and at the > sametime sterror in "error.log", it's possible? [...] { cmd 2>&1 >&3 | tee -a error.log >&3; } 3>> log.txt -- Stéphane |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
In article <slrnftij2e.9do.stephane.chazelas@spam.is.invalid> ,
Stephane CHAZELAS <this.address@is.invalid> wrote: >2008-03-13, 14:13(+00), luciosan: >[...] >> How can I redirect stout and sterror in a file and only sterror >> inanother file at the same time? >> >> For example, If I run the script: >> /script.sh >> log.txt 2>&1 >> >> It will write in "log.txt" stout and sterror. >> >> But, If I want write stout and sterror in "log.txt" and at the >> sametime sterror in "error.log", it's possible? >[...] > >{ cmd 2>&1 >&3 | tee -a error.log >&3; } 3>> log.txt > >-- >Stéphane Or you could do it the easy way (some will say this doesn't preserve the order of the lines, but my bet is that's not that important in the long run - plus note that buffering considerations may render the previously given solutions also somewhat inderminate in outcome): cmd > file1 2> file2 cat file2 >> file1 Or, you could use Expect - which is what I would do if I really cared about the "look" of the output. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Jeenu wrote:
> ./script.sh 2>err | tee out >> err > > Now the file "err" should have both stdout and stderr. Meantime, the > file out should contain your stdout only. No, the err file is likely to be missing some or all of stdout. That's because the file is opened independently in two processes, and only one of them opens it in append mode. For example: $ (echo OUT; sleep 2; echo ERR >&2) 2>err | tee out >> err $ cat err ERR $ cat out OUT To avoid data loss, both processes must open the file in append mode: $ > err $ (echo OUT; sleep 2; echo ERR >&2) 2>>err | tee out >> err $ cat err OUT ERR $ cat out OUT Also, this doesn't quite match what the OP asked for, which would be: $ > out $ (echo OUT; sleep 2; echo ERR >&2) 2>&1 >>out | tee err >> out $ cat out OUT ERR $ cat err ERR -- Geoff Clare <netnews@gclare.org.uk> |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
2008-03-14, 14:42(+00), Geoff Clare:
[...] > Also, this doesn't quite match what the OP asked for, which would be: > > $ > out > $ (echo OUT; sleep 2; echo ERR >&2) 2>&1 >>out | tee err >> out > $ cat out > OUT > ERR > $ cat err > ERR And you can do without opening "out" twice as I already indicated, which removes the need for opening in append mode: { cmd 2>&1 >&3 3>&- | tee err >&3 3>&-; } 3> out -- Stéphane |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
In article
<frbm6e$5fr$2@news.xmission.com>gazelle@xmission.x mission.com (Kenny McCormack) wrote: > In article <slrnftij2e.9do.stephane.chazelas@spam.is.invalid> , > Stephane CHAZELAS <this.address@is.invalid> wrote: >> { cmd 2>&1 >&3 | tee -a error.log >&3; } 3>> log.txt I tried your command, but the log.txt file contains sterror and stout not in order.In other words, I wrote a script to generate: 1->error 1->out 2->error 2->out 3->error 3->out 4->error 4->out and I've redirect the error in sterror and in a file (error.log); and sterror+stout in another file (out.log) The result is: "cat error.log" 1->error 2->error 3->error 4->error and it's ok but "cat out.log" contains 1->out 2->out 3->out 4->out 1->error 2->error 3->error 4->error As you can see, the out.log contains sterror+stout but not in order. -- I'm using an evaluation license of nemo since 204 days. You should really try it! http://www.malcom-mac.com/nemo |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
2008-03-14, 16:13(+00), luciosan:
[...] >>> { cmd 2>&1 >&3 | tee -a error.log >&3; } 3>> log.txt > I tried your command, but the log.txt file contains sterror and stout > not in order.In other words, I wrote a script to generate: > 1->error > 1->out > 2->error > 2->out > 3->error > 3->out > 4->error > 4->out > > and I've redirect the error in sterror and in a file (error.log); and > sterror+stout in another file (out.log) > > The result is: [...] > but "cat out.log" contains > 1->out > 2->out > 3->out > 4->out > 1->error > 2->error > 3->error > 4->error > > As you can see, the out.log contains sterror+stout but not in order. [...] Above, you have too applications running concurrently (cmd and tee) and writing to a same file. There's little chance that you'll have them synchronise themselves so as to get the order you wish. Note that I forgot to mention zsh: setopt mult_ios cmd > log.txt 2>&1 2> error.log But you're likely to have the same problem. Here, the teeing is performed internally by zsh. -- Stéphane |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
In article <slrnftlam6.dp1.stephane.chazelas@spam.is.invali d>
StephaneCHAZELAS <this.address@is.invalid> wrote: > [CUT] > setopt mult_ios > cmd > log.txt 2>&1 2> error.log This is my test. zsh setopt mult_ios COUNTER=1 while [ $COUNTER -lt 11 ]; do echo "${COUNTER} error" >&2 echo "${COUNTER} out" >&1 let COUNTER=COUNTER+1 done > log.txt 2>&1 2> error.log The output on screen without redirect (without "> log.txt 2>&1 2> error.log"):1 error 1 out 2 error 2 out 3 error 3 out 4 error 4 out 5 error 5 out 6 error 6 out 7 error 7 out 8 error 8 out 9 error 9 out 10 error 10 out The output with your redirect: cat log.txt 1 out 1 error 2 out 3 out 4 out 5 out 6 out 7 out 8 out 9 out 10 out 2 error 3 error 4 error 5 error 6 error 7 error 8 error 9 error 10 error cat error.txt 1 error 2 error 3 error 4 error 5 error 6 error 7 error 8 error 9 error 10 error As you can see, the file log.txt is not similare the output without redirect... What do you think about? -- I'm using an evaluation license of nemo since 205 days. You should really try it! http://www.malcom-mac.com/nemo |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
2008-03-14, 19:20(+00), luciosan:
[...] > As you can see, the file log.txt is not similare the output without > redirect... > > What do you think about? [...] Same as for tee: two processes writing concurrently to a file (this time cmd and zsh) this will depend on many things like timing and system scheduling. You'd need a single process to take both the cmd's output and errors, and even then, pipes won't do as pipes have buffers which cause the writer not to block until the reader has read Typically, if you have "cmd" doing: echo foo echo bar >&2 echo baz and have cmd's stdout and stderr going to 2 pipes to a "processing" command. The system may let "cmd" output "foo", "bar" and "baz" before "processing" has even started to read from any of its pipes. Then "processing" has no way to know that it needs to read first one line from its first pipe, then one from its second, then another from the first... -- Stéphane |
|
![]() |
| Outils de la discussion | |
|
|