问题
I'm writing a script which does something like the following:
echo -n "Doing stuff, wait for it... "
do_stuff
(($?==0)) && echo SUCCESS || echo FAILURE
Excuse the poor bash skills. Anyway, the problem is that the first part of the line doesn't get printed until do_stuff
is done - while it is important to me the user know what I'm running next. It is also important to me, since I'm pedantic, not to print a newline. So, the text is in the buffer and doesn't get flushed.
This question is very similar, but OP there was satisfied with, well, the way things are basically. I'm not. If push comes to shove I'm even willing to use something curses-related (but remember this is a shell script after all).
回答1:
I think the appropriate thing to do is to turn off buffering:
stdbuf -i0 -o0 -e0 <command>
i = stdin
o = stdout
e = stderr
来源:https://stackoverflow.com/questions/36847897/in-bash-how-can-i-force-a-flush-of-an-incomplete-line-printed-to-the-terminal