In bash, How can I force a flush of an incomplete line printed to the terminal

夙愿已清 提交于 2019-12-09 19:29:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!