Results 1 to 6 of 6

Thread: How to do this

  1. #1
    Join Date
    Jan 2004
    Posts
    10

    Unanswered: 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) ?

  2. #2
    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

  3. #3
    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.

    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

  4. #4
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525

    Re: Thank you

    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

  5. #5
    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
    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

  6. #6
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525

    Re: Thank you

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •