I want to pipe stdout of a program to both a file to store the output of the program and to the console in order to be able to observe the progress of the program. Unfortunatelly bash does not allow splitting pipes out of the box. So I just hacked this little program in order to accomplish that. It is quite simple - all it does is take stdin and write it to stdout and stderr. That way one can be piped to a file and the other will be appearing on the console.

splitstdin.c:

C:
  1. #include <stdio.h>
  2. #define BUF_LEN 255
  3. main(int argc, char** argv)
  4. {
  5.     size_t numRead;
  6.     char buf[BUF_LEN + 1];
  7.     size_t charSize = sizeof(char);
  8.     while (!feof(stdin)) {
  9.         numRead = fread(buf, charSize, BUF_LEN, stdin);
  10.         buf[numRead] = '\0'/* append a nul character */
  11.         fwrite(buf, charSize, numRead, stdout);
  12.         fwrite(buf, charSize, numRead, stderr);
  13.     }
  14. }

For stdout AND stderr of the program "programtoobserve" to get parsed by splitstdin use this commandform in bash:
split to file and console:

programtoobserve 2>&1 | splitstdin 2>backupOutput

split to two files:

programtoobserve 2>&1 | splitstdin 1>backupOutput1 2>backupOutput2

Edit:
I found the proper built-in command: tee