PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > One file for "stout+sterror" and another for "sterror"; it'spossible?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

One file for "stout+sterror" and another for "sterror"; it'spossible?

Réponse
 
LinkBack Outils de la discussion
Vieux 13/03/2008, 15h13   #1
luciosan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut One file for "stout+sterror" and another for "sterror"; it'spossible?

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

  Réponse avec citation
Vieux 13/03/2008, 16h15   #2
Teni
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: One file for "stout+sterror" and another for "sterror"; it'spossible?

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...
  Réponse avec citation
Vieux 13/03/2008, 16h37   #3
Jeenu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: One file for "stout+sterror" and another for "sterror"; it'spossible?

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
  Réponse avec citation
Vieux 13/03/2008, 16h45   #4
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: One file for "stout+sterror" and another for "sterror"; it's possible?

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
  Réponse avec citation
Vieux 13/03/2008, 17h53   #5
Kenny McCormack
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: One file for "stout+sterror" and another for "sterror"; it's possible?

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.

  Réponse avec citation
Vieux 14/03/2008, 15h42   #6
Geoff Clare
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: One file for "stout+sterror" and another for "sterror"; it'spossible?

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>
  Réponse avec citation
Vieux 14/03/2008, 17h02   #7
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: One file for "stout+sterror" and another for "sterror"; it's possible?

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
  Réponse avec citation
Vieux 14/03/2008, 17h13   #8
luciosan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: One file for "stout+sterror" and another for "sterror"; it'spossible?

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

  Réponse avec citation
Vieux 14/03/2008, 17h39   #9
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: One file for "stout+sterror" and another for "sterror"; it's possible?

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
  Réponse avec citation
Vieux 14/03/2008, 20h20   #10
luciosan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: One file for "stout+sterror" and another for "sterror"; it'spossible?

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

  Réponse avec citation
Vieux 14/03/2008, 21h12   #11
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: One file for "stout+sterror" and another for "sterror"; it's possible?

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h47.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20402 seconds with 19 queries