Question:
I have a simple C++ program that counts from 0 to 10 with an increment every 1 second. When the value is incremented, it is written to stdout. This program intentionally uses printf rather than std::cout. I want to call this program from a bash script, and perform some function (eg echo) on the value when it is written to stdout. However, my script waits for the program to terminate, and then process all the values at the same time.C++ prog:
Many thanks, Stuart
Answer:
As you correctly observed,$(command)
waits for the entire output of command
, splits that output, and only after that, the for
loop starts.To read output as soon as is available, use
while read
:<()
while IFS= read -r line; do
echo “do stuff with $line”
done < <(./script-test)
# do more stuff, that depends on variables set inside the loop
[/code]
If you have better answer, please add a comment about this, thank you!