Bash script – run application and show live output

applicationbashoutput

I've got a bash script that sets up some environment parameters and then runs a c++ application.

The c++ application outputs information showing it's current state as it runs.

Currently, I only see the results of this once it's finished running. (approx 1 min) Is there anyway to have the bash script show the live output from the application ?

This is what I have so far:

OUTPUT="$(sudo ./test_app -release)"    
echo $OUTPUT

I get the following once the application has completed.:

release acknowledgereleasingstage1stage2released

If I ran the application direct from the command line, I'd get this as new line as each process completed, not all in one when the application completed.

release acknowledge
releasing
stage1
stage2
released

Any ideas how to do this ? I'd like to call the app from the bash script to save the users having to run multiple commands.

Thanks.

Best Answer

If you want to show the output and don't need to save it, just run the command:

sudo ./test_app -release

If you want to both save the output and display it, call tee to duplicate the output.

OUTPUT=$(sudo ./test_app -release | tee /dev/stderr)

When you redirect the output, the application may buffer its output in large chunks instead of line by line. Line by line is the default only when the output is a terminal, not when it's a pipe or a regular file. If this is the case with your application, call unbuffer or stdbuff. Due to the way they work, stdbuf needs to be invoked by sudo, whereas buffer can run as you.

OUTPUT=$(sudo stdbuf ./test_app -release | tee /dev/stderr)
OUTPUT=$(unbuffer sudo ./test_app -release | tee /dev/stderr)
Related Question