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 > piping stdout and stderr to different processes?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

piping stdout and stderr to different processes?

Réponse
 
LinkBack Outils de la discussion
Vieux 24/07/2007, 21h58   #1
Neil Cherry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut piping stdout and stderr to different processes?

I've found name pipes (fifos) but I am confused on using them
properly. What I want to do is to take the stdout of a process and
send it to another process to be filtered. I also want to take the
stderr of the first process and send it to another process to
also be filtered. Any examples?

Thanks (an no it's not for school).

--
Linux Home Automation Neil Cherry ncherry@linuxha.com
http://www.linuxha.com/ Main site
http://linuxha.blogspot.com/ My HA Blog
Author of: Linux Smart Homes For Dummies
  Réponse avec citation
Vieux 25/07/2007, 01h01   #2
jellybean stonerfish
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping stdout and stderr to different processes?

On Tue, 24 Jul 2007 15:58:52 -0500, Neil Cherry wrote:

> I've found name pipes (fifos) but I am confused on using them
> properly. What I want to do is to take the stdout of a process and
> send it to another process to be filtered. I also want to take the
> stderr of the first process and send it to another process to
> also be filtered. Any examples?
>
> Thanks (an no it's not for school).
>


Maybe this simple example will . I will create two named pipes.
Then I will in the background, cat these pipes to files. Next I will
create a file named "afile". To make it all happen I do a directory
listing of afile and notafile (notafile does not exist and should give an
error) and redirect the output to the named pipes. Finally I will cat out
the files with the data that the background cats read from the fifos.
First a cut and paste of my terminal, following is a breakdown of what
happens.

$ mkfifo stderrpipe
$ mkfifo stdoutpipe
$ cat stderrpipe > errorlog &
[1] 5860
$ cat stdoutpipe > outlog &
[2] 5863
$ touch afile
$ ls afile notafile > stdoutpipe 2> stderrpipe
[1]- Done cat stderrpipe > errorlog
[2]+ Done cat stdoutpipe > outlog
$ cat errorlog
ls: notafile: No such file or directory
$ cat outlog
afile


DEEPER EXPLANATIONS FOLLOW

CREATE PIPE FOR ERROR
$ mkfifo stderrpipe

CREATE PIPE FOR STDOUT
$ mkfifo stdoutpipe

CAT THE ERROR PIPE TO A FILE IN BACKGROUND
$ cat stderrpipe > errorlog &
BACKGROUND PROCESS #1
[1] 5860

CAT THE STDOUT PIPE TO A FILE IN BACKGROUND
$ cat stdoutpipe > outlog &
BACKGROUND PROCESS #2
[2] 5863

CREATE A FILE "afile"
$ touch afile

REDIRECT THE OUTPUT OF ls TO THE PIPES
$ ls afile notafile > stdoutpipe 2> stderrpipe

BOTH BACKGROUND PROCESSES FINISH WHEN THEIR
PIPES GET TO END OF FILE. THAT IS NOW, BECAUSE
THE LS COMMAND IS FINISHED GIVING THEM DATA

[1]- Done cat stderrpipe > errorlog
[2]+ Done cat stdoutpipe > outlog

OUTPUT THE FILES CREATED BY THE ABOVE cats
$ cat errorlog
ls: notafile: No such file or directory
$ cat outlog
afile

I hope that s.

stonerfish
  Réponse avec citation
Vieux 25/07/2007, 01h01   #3
jellybean stonerfish
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping stdout and stderr to different processes?

On Tue, 24 Jul 2007 15:58:52 -0500, Neil Cherry wrote:

> I've found name pipes (fifos) but I am confused on using them
> properly. What I want to do is to take the stdout of a process and
> send it to another process to be filtered. I also want to take the
> stderr of the first process and send it to another process to
> also be filtered. Any examples?
>
> Thanks (an no it's not for school).
>


Maybe this simple example will . I will create two named pipes.
Then I will in the background, cat these pipes to files. Next I will
create a file named "afile". To make it all happen I do a directory
listing of afile and notafile (notafile does not exist and should give an
error) and redirect the output to the named pipes. Finally I will cat out
the files with the data that the background cats read from the fifos.
First a cut and paste of my terminal, following is a breakdown of what
happens.

$ mkfifo stderrpipe
$ mkfifo stdoutpipe
$ cat stderrpipe > errorlog &
[1] 5860
$ cat stdoutpipe > outlog &
[2] 5863
$ touch afile
$ ls afile notafile > stdoutpipe 2> stderrpipe
[1]- Done cat stderrpipe > errorlog
[2]+ Done cat stdoutpipe > outlog
$ cat errorlog
ls: notafile: No such file or directory
$ cat outlog
afile


DEEPER EXPLANATIONS FOLLOW

CREATE PIPE FOR ERROR
$ mkfifo stderrpipe

CREATE PIPE FOR STDOUT
$ mkfifo stdoutpipe

CAT THE ERROR PIPE TO A FILE IN BACKGROUND
$ cat stderrpipe > errorlog &
BACKGROUND PROCESS #1
[1] 5860

CAT THE STDOUT PIPE TO A FILE IN BACKGROUND
$ cat stdoutpipe > outlog &
BACKGROUND PROCESS #2
[2] 5863

CREATE A FILE "afile"
$ touch afile

REDIRECT THE OUTPUT OF ls TO THE PIPES
$ ls afile notafile > stdoutpipe 2> stderrpipe

BOTH BACKGROUND PROCESSES FINISH WHEN THEIR
PIPES GET TO END OF FILE. THAT IS NOW, BECAUSE
THE LS COMMAND IS FINISHED GIVING THEM DATA

[1]- Done cat stderrpipe > errorlog
[2]+ Done cat stdoutpipe > outlog

OUTPUT THE FILES CREATED BY THE ABOVE cats
$ cat errorlog
ls: notafile: No such file or directory
$ cat outlog
afile

I hope that s.

stonerfish
  Réponse avec citation
Vieux 25/07/2007, 10h27   #4
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping stdout and stderr to different processes?

2007-07-24, 15:58(-05), Neil Cherry:
> I've found name pipes (fifos) but I am confused on using them
> properly. What I want to do is to take the stdout of a process and
> send it to another process to be filtered. I also want to take the
> stderr of the first process and send it to another process to
> also be filtered. Any examples?

[...]

No need for named pipes here.

{
{
cm1 3>&- |
cmd2 2>&3 3>&-
} 2>&1 >&4 4>&- |
cmd3 3>&- 4>&-
} 3>&2 4>&1


--
Stéphane
  Réponse avec citation
Vieux 25/07/2007, 10h27   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping stdout and stderr to different processes?

2007-07-24, 15:58(-05), Neil Cherry:
> I've found name pipes (fifos) but I am confused on using them
> properly. What I want to do is to take the stdout of a process and
> send it to another process to be filtered. I also want to take the
> stderr of the first process and send it to another process to
> also be filtered. Any examples?

[...]

No need for named pipes here.

{
{
cm1 3>&- |
cmd2 2>&3 3>&-
} 2>&1 >&4 4>&- |
cmd3 3>&- 4>&-
} 3>&2 4>&1


--
Stéphane
  Réponse avec citation
Vieux 27/07/2007, 02h41   #6
Neil Cherry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping stdout and stderr to different processes?

On Wed, 25 Jul 2007 00:01:40 GMT, jellybean stonerfish wrote:
> On Tue, 24 Jul 2007 15:58:52 -0500, Neil Cherry wrote:
>
>> I've found name pipes (fifos) but I am confused on using them
>> properly. What I want to do is to take the stdout of a process and
>> send it to another process to be filtered. I also want to take the
>> stderr of the first process and send it to another process to
>> also be filtered. Any examples?
>>
>> Thanks (an no it's not for school).
>>

>
> Maybe this simple example will . I will create two named pipes.
> Then I will in the background, cat these pipes to files. Next I will
> create a file named "afile". To make it all happen I do a directory
> listing of afile and notafile (notafile does not exist and should give an
> error) and redirect the output to the named pipes. Finally I will cat out
> the files with the data that the background cats read from the fifos.
> First a cut and paste of my terminal, following is a breakdown of what
> happens.
>
> $ mkfifo stderrpipe
> $ mkfifo stdoutpipe
> $ cat stderrpipe > errorlog &
> [1] 5860
> $ cat stdoutpipe > outlog &
> [2] 5863
> $ touch afile
> $ ls afile notafile > stdoutpipe 2> stderrpipe
> [1]- Done cat stderrpipe > errorlog
> [2]+ Done cat stdoutpipe > outlog
> $ cat errorlog
> ls: notafile: No such file or directory
> $ cat outlog
> afile
>
>
> DEEPER EXPLANATIONS FOLLOW
>
> CREATE PIPE FOR ERROR
> $ mkfifo stderrpipe
>
> CREATE PIPE FOR STDOUT
> $ mkfifo stdoutpipe
>
> CAT THE ERROR PIPE TO A FILE IN BACKGROUND
> $ cat stderrpipe > errorlog &
> BACKGROUND PROCESS #1
> [1] 5860
>
> CAT THE STDOUT PIPE TO A FILE IN BACKGROUND
> $ cat stdoutpipe > outlog &
> BACKGROUND PROCESS #2
> [2] 5863
>
> CREATE A FILE "afile"
> $ touch afile
>
> REDIRECT THE OUTPUT OF ls TO THE PIPES
> $ ls afile notafile > stdoutpipe 2> stderrpipe
>
> BOTH BACKGROUND PROCESSES FINISH WHEN THEIR
> PIPES GET TO END OF FILE. THAT IS NOW, BECAUSE
> THE LS COMMAND IS FINISHED GIVING THEM DATA
>
> [1]- Done cat stderrpipe > errorlog
> [2]+ Done cat stdoutpipe > outlog
>
> OUTPUT THE FILES CREATED BY THE ABOVE cats
> $ cat errorlog
> ls: notafile: No such file or directory
> $ cat outlog
> afile
>
> I hope that s.
>
> stonerfish


Thanks! That s big time.

--
Linux Home Automation Neil Cherry ncherry@linuxha.com
http://www.linuxha.com/ Main site
http://linuxha.blogspot.com/ My HA Blog
Author of: Linux Smart Homes For Dummies
  Réponse avec citation
Vieux 27/07/2007, 02h44   #7
Neil Cherry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping stdout and stderr to different processes?

On Wed, 25 Jul 2007 09:27:33 GMT, Stephane CHAZELAS wrote:
> 2007-07-24, 15:58(-05), Neil Cherry:
>> I've found name pipes (fifos) but I am confused on using them
>> properly. What I want to do is to take the stdout of a process and
>> send it to another process to be filtered. I also want to take the
>> stderr of the first process and send it to another process to
>> also be filtered. Any examples?

> [...]
>
> No need for named pipes here.
>
> {
> {
> cm1 3>&- |
> cmd2 2>&3 3>&-
> } 2>&1 >&4 4>&- |
> cmd3 3>&- 4>&-
> } 3>&2 4>&1


Thanks.

OK now I see why I didn't get it to work. I didn't try that far.
But I have to say I'm not quite sure what I'm reading just yet.
I'll have to hit the man pages as I'm not used to the >&-
syntax.

--
Linux Home Automation Neil Cherry ncherry@linuxha.com
http://www.linuxha.com/ Main site
http://linuxha.blogspot.com/ My HA Blog
Author of: Linux Smart Homes For Dummies
  Réponse avec citation
Vieux 27/07/2007, 09h12   #8
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping stdout and stderr to different processes?

On 27 Jul., 03:44, Neil Cherry <n...@.uucp> wrote:
> On Wed, 25 Jul 2007 09:27:33 GMT, Stephane CHAZELAS wrote:
> > 2007-07-24, 15:58(-05), Neil Cherry:
> >> I've found name pipes (fifos) but I am confused on using them
> >> properly. What I want to do is to take the stdout of a process and
> >> send it to another process to be filtered. I also want to take the
> >> stderr of the first process and send it to another process to
> >> also be filtered. Any examples?

> > [...]

>
> > No need for named pipes here.

>
> > {
> > {
> > cm1 3>&- |
> > cmd2 2>&3 3>&-
> > } 2>&1 >&4 4>&- |
> > cmd3 3>&- 4>&-
> > } 3>&2 4>&1

>
> Thanks.
>
> OK now I see why I didn't get it to work. I didn't try that far.
> But I have to say I'm not quite sure what I'm reading just yet.
> I'll have to hit the man pages as I'm not used to the >&-
> syntax.


N>&- close the file descriptor with number N.

Janis

>
> --
> Linux Home Automation Neil Cherry nche...@linuxha.comhttp://www.linuxha.com/ Main sitehttp://linuxha.blogspot.com/ My HA Blog
> Author of: Linux Smart Homes For Dummies



  Réponse avec citation
Vieux 27/07/2007, 10h08   #9
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping stdout and stderr to different processes?

2007-07-26, 20:44(-05), Neil Cherry:
[...]
>> {
>> {
>> cm1 3>&- |
>> cmd2 2>&3 3>&-
>> } 2>&1 >&4 4>&- |
>> cmd3 3>&- 4>&-
>> } 3>&2 4>&1

>
> Thanks.
>
> OK now I see why I didn't get it to work. I didn't try that far.
> But I have to say I'm not quite sure what I'm reading just yet.
> I'll have to hit the man pages as I'm not used to the >&-
> syntax.


3>&- is for closing fd 3. It's not necessary, but it's for tidy
up. None of the commands will ever try (not should they) to
access the fd 3 and 4, so it's best to close them before
executing those commands so that they can use those fds for
something else.

{
{
cm1 |
cmd2 2>&3
} 2>&1 >&4 |
cmd3
} 3>&2 4>&1

is functionnaly equivalent.

if cmd2 doesn't output anything on its stdout nor stderr, it can
even be simplified to:
{ cm1 | cmd2; } 2>&1 | cmd3

Or if you want to be sure:

{ cm1 | cmd2 > /dev/null 2>&1; } 2>&1 | cmd3

--
Stéphane
  Réponse avec citation
Vieux 27/07/2007, 21h49   #10
Neil Cherry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: piping stdout and stderr to different processes?

On Fri, 27 Jul 2007 09:08:23 GMT, Stephane CHAZELAS wrote:
> 2007-07-26, 20:44(-05), Neil Cherry:
> [...]
>>> {
>>> {
>>> cm1 3>&- |
>>> cmd2 2>&3 3>&-
>>> } 2>&1 >&4 4>&- |
>>> cmd3 3>&- 4>&-
>>> } 3>&2 4>&1

>>
>> Thanks.
>>
>> OK now I see why I didn't get it to work. I didn't try that far.
>> But I have to say I'm not quite sure what I'm reading just yet.
>> I'll have to hit the man pages as I'm not used to the >&-
>> syntax.

>
> 3>&- is for closing fd 3. It's not necessary, but it's for tidy
> up. None of the commands will ever try (not should they) to
> access the fd 3 and 4, so it's best to close them before
> executing those commands so that they can use those fds for
> something else.
>
> {
> {
> cm1 |
> cmd2 2>&3
> } 2>&1 >&4 |
> cmd3
> } 3>&2 4>&1
>
> is functionnaly equivalent.
>
> if cmd2 doesn't output anything on its stdout nor stderr, it can
> even be simplified to:
> { cm1 | cmd2; } 2>&1 | cmd3
>
> Or if you want to be sure:
>
> { cm1 | cmd2 > /dev/null 2>&1; } 2>&1 | cmd3
>


Thanks that s. :-)

--
Linux Home Automation Neil Cherry ncherry@linuxha.com
http://www.linuxha.com/ Main site
http://linuxha.blogspot.com/ My HA Blog
Author of: Linux Smart Homes For Dummies
  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 05h54.


É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,20654 seconds with 18 queries