If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > How to do this

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-22-04, 17:15
nic39 nic39 is offline
Registered User
 
Join Date: Jan 2004
Posts: 10
How to do this

I want to do the following:

STDOUT of cmd1 pipes to cmd2
STDERR of cmd1 pipes to cmd3

Is there a simple way of doing this in unix shell (without creating named
fifo) ?
Reply With Quote
  #2 (permalink)  
Old 01-29-04, 13:13
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
This looks messy but it is straight forward once you get your head round it...

{ CMD1 2>&1 1>aTempFile | CMD2; cat aTempFile; } | CMD3

2>&1 : tell file descriptor 2 (fd2, i.e. std error) to go to the same place that fd1 (i.e. std output) would go. Stdout would normally go down a pipe, so in this instance stderr would also go down the pipe.

1>aTempFile : redirect fd1 (i.e. std output) to a file called 'aTempFile'. Note that fd2 at this point is still directing its output to the same place fd1 was originally going and not to 'aTempFile'.

The use of the pipe to CMD2 will spawn a subshell, so as a fortunate consequence, you won't have to reset the file descriptors to their original states as these will remain unchanged in the parent shell.

I couldn't think of a way to get round the use of a temporary file unfortunately.

Damian
Reply With Quote
  #3 (permalink)  
Old 01-29-04, 17:34
nic39 nic39 is offline
Registered User
 
Join Date: Jan 2004
Posts: 10
Talking Thank you

Thank you.
I do however think that tmp file is a problem, in my case.
I also did not want to merge stdout and stderr.

Quote:
Originally posted by Damian Ibbotson
This looks messy but it is straight forward once you get your head round it...

{ CMD1 2>&1 1>aTempFile | CMD2; cat aTempFile; } | CMD3

2>&1 : tell file descriptor 2 (fd2, i.e. std error) to go to the same place that fd1 (i.e. std output) would go. Stdout would normally go down a pipe, so in this instance stderr would also go down the pipe.

1>aTempFile : redirect fd1 (i.e. std output) to a file called 'aTempFile'. Note that fd2 at this point is still directing its output to the same place fd1 was originally going and not to 'aTempFile'.

The use of the pipe to CMD2 will spawn a subshell, so as a fortunate consequence, you won't have to reset the file descriptors to their original states as these will remain unchanged in the parent shell.

I couldn't think of a way to get round the use of a temporary file unfortunately.

Damian
Reply With Quote
  #4 (permalink)  
Old 01-29-04, 20:04
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Re: Thank you

Quote:
Originally posted by nic39
Thank you.
I do however think that tmp file is a problem, in my case.
I also did not want to merge stdout and stderr.
First off, 2>&1 does not *merge* stdout and stderr, it simply tells the shell that fd2 should adopt the behaviour of fd1. Often this would mean that stdout and stderr would in fact be merged but if you're trying to do what we are here and direct stderr down a pipe, you would have to get fd2 to temporarily behave like fd1, while telling fd1 to behave like another file descriptor (or to go to a file).

Anyhow, the original solution I gave you is flawed. If CMD2 has std output, this would be passed down the pipe to CMD3 which we don't want. I tested using 'mail', so I didn't notice - Ooops!

This should work though (and it doesn't use a temp file!)...
Code:
{ CMD1 | CMD2 2> /dev/null; } 3>&2 2>&1 1>&3 | CMD3
Here we introduce fd3 so that we can swap the behaviour of fd1 and fd2 around. You might want to consider where you send stderr from CMD2 'cos I don't think you'd really want it going to /dev/null.

Damian
Reply With Quote
  #5 (permalink)  
Old 01-30-04, 13:08
nic39 nic39 is offline
Registered User
 
Join Date: Jan 2004
Posts: 10
Thumbs up Re: Thank you

Thanks a lot!
This probably going to work! (I have to try )

Code:
{ CMD1 | CMD2 ; } 3>&2 2>&1 1>&3 3>&- | CMD3
Quote:
Originally posted by Damian Ibbotson
First off, 2>&1 does not *merge* stdout and stderr, it simply tells the shell that fd2 should adopt the behaviour of fd1. Often this would mean that stdout and stderr would in fact be merged but if you're trying to do what we are here and direct stderr down a pipe, you would have to get fd2 to temporarily behave like fd1, while telling fd1 to behave like another file descriptor (or to go to a file).

Anyhow, the original solution I gave you is flawed. If CMD2 has std output, this would be passed down the pipe to CMD3 which we don't want. I tested using 'mail', so I didn't notice - Ooops!

This should work though (and it doesn't use a temp file!)...
Code:
{ CMD1 | CMD2 2> /dev/null; } 3>&2 2>&1 1>&3 | CMD3
Here we introduce fd3 so that we can swap the behaviour of fd1 and fd2 around. You might want to consider where you send stderr from CMD2 'cos I don't think you'd really want it going to /dev/null.

Damian
Reply With Quote
  #6 (permalink)  
Old 01-31-04, 13:53
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Re: Thank you

Quote:
Originally posted by nic39
Thanks a lot!
This probably going to work! (I have to try )

Code:
{ CMD1 | CMD2 ; } 3>&2 2>&1 1>&3 3>&- | CMD3
Again, you need to give consideration to stderr from CMD2. In the above this will be piped into CMD3. You could always introduce fd4 and do something wonderfully complex but I'll leave that up to you ;-)

It is probably good practice to close fd3 as you have done. I'm not sure if it necessary here though because the pipeline will be in a subshell, so fd3 will not exist outside of it.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On