Advanced shell scripting question

jim.mcginness at att.net jim.mcginness at att.net
Thu Sep 5 18:12:46 EDT 2002


Kevin D. Clark wrote:

> This doesn't work either.  Try using the following program as input to
> such a scheme:

     #include <stdio.h>
     
     int
     main(int argc, char *argv[])
     {
       int i;
     
       for (i=0; i<100000; i+=2) {
         printf("%d\n", i);
         fprintf(stderr, "%d\n", i+1);
       }
     }

True enough. My test case was, unbeknownst to me, being line-buffered. If you install 'expect', you can do this:

#! /bin/bash

mkfifo /tmp/fifo1-$$;mkfifo /tmp/fifo2-$$
tee errlog </tmp/fifo2-$$ >/tmp/fifo1-$$ &
tee outnerr </tmp/fifo1-$$ &
unbuffer ./testprog "2>/tmp/fifo2-$$" 1>/tmp/fifo1-$$
rm /tmp/fifo[12]-$$

The 'unbuffer' script (usually) fools the stdio library into making stdout line-buffered because it's a pty. The stderr is already unbuffered -- and we've pushed the redirection down into the script -- so you can get strict alternation between the lines written to stderr with lines written to stdout.

It's certainly a lot of extra apparatus to go through!



More information about the gnhlug-discuss mailing list