6

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.

  • 2
    Replace OUTPUT="$(sudo ./test_app -release)" with sudo ./test_app -release in your script? – EightBitTony Feb 26 '16 at 15:22
  • Thanks that seems to work... not sure why I didn't think of doing that ! – user214292 Feb 26 '16 at 15:59
  • Alternatively, if you want to store the output for rebroadcast later (or processing): SCRATCH=$(mktemp); ./test_app -release > $SCRATCH 2>&1; [other stuff]; cat $SCRATCH; rm $SCRATCH – DopeGhoti Feb 26 '16 at 16:48
3

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)
2

Assuming you don't need to store the output for any specific reason, you can simply replace,

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

with

sudo ./test_app -release

There's no special difference in shell scripts between built in commands and non-built in commands (at the basic level). They all just execute and output stuff.

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.