September 27th, 2007
Redirection Puzzle
Today’s entry here at cosine.org is a little Unix puzzle for everyone to work. I came up with this seven years ago when I was first reading about the Plan 9 shell, rc. Plan 9’s shell has a nifty little syntax for connecting a pipe from an arbitrary file descriptor of one process to any file descriptor of another process when they are invoked at the command line together. Using this syntax we can pipe standard error to a different process than we pipe standard output to:
; {puzzle2000 | tee out.log} |[2] tee err.log This is standard output This is standard error ; cat out.log This is standard output ; cat err.log This is standard error
As seen above, the puzzle2000 script outputs two lines. It outputs “This is standard output
” to standard output and “This is standard error
” to standard error. The challenge is repeating the above Plan 9 shell functionality using the Bourne shell instead. Back in 2000 it took me about two weeks of throwing ideas around to work out a solution. This is non-trivial because the Bourne shell’s pipe syntax can only connect the pipe from standard output of the first process to standard input of the second. So can you figure out how to extend this functionality? Give it a try!
Please do not post answers or spoilers in the comments of this post. I have set up an additional post to discuss possible answers and to avoid spoiling it for those that want to work out the challenge. I have also provided a little bit of information about my own solution there, and will eventually update that post with the solution itself. Feel free to post your answers in the comments there (do not read them if you do not want to be spoiled!). Also, if you do not want to register for an account you can email your answers or questions to cosine at cosine dot org, and I will post them if they are good (that is, if they would contribute to a positive discussion) even if they are not correct.
Note that I will delete comments below if they are a spoiler or if they discuss possible answers, so please limit the discussion here to clarification about what the challenge is or other general comments.