3

I have a (proprietary) program which I start with ./program -a asdf in the Terminal. When I do that, I can see all the output at the right time.

However, when I want to use the programs' output in a script of mine and run ./program -a asdf | script.sh, with "script.sh" just containing read line and echo $line in a loop, the output of the program is "weird". When the program prints the first block of lines, the script doesn't print anything. When the program (a few minutes later) prints the 2nd block of lines, my script will print the first block.

Why?

Is there a way for my own script to "act like a terminal" to get the output as soon as it would show up in the terminal?

1

The problem is that ./program -a asdf's output gets buffered when run in the pipeline, and that any output it's going to sit within the buffer until this one gets full.

You can control a command's output buffering using stdbuf; in particular, to run ./program -a asdf with an unbuffered stdout:

stdbuf -o0 ./program -a asdf

So that your buffered pipeline becomes:

stdbuf -o0 ./program -a asdf | script.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.