|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|