Bash script - run application and show live output
Solution 1
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)
Solution 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.
Related videos on Youtube
user214292
Updated on September 18, 2022Comments
-
user214292 over 1 year
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.
-
Admin about 8 yearsReplace
OUTPUT="$(sudo ./test_app -release)"
withsudo ./test_app -release
in your script? -
Admin about 8 yearsThanks that seems to work... not sure why I didn't think of doing that !
-
Admin about 8 yearsAlternatively, 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
-