8

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

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Regarding your bash skills, nothing's wrong with them; the construct in the last line is entirely idiomatic, as far as the arithmetic comparison goes. The only suggestion to simplify the code is combine the last 2 lines into `do_stuff && echo SUCCESS || echo FAILURE`. But oftentimes, when there is a requirement to compare `$?` with anything other than 0, this is how it is used (e. g., when timeout(1) exits with status 124 on timeout, and you need to handle this case specially). – kkm inactive - support strike Jul 28 '19 at 07:34

1 Answers1

6

I think the appropriate thing to do is to turn off buffering:

stdbuf -i0 -o0 -e0 <command>


i = stdin
o = stdout
e = stderr

in your specific case you would only need -o0 for the standard output.

ichramm
  • 6,437
  • 19
  • 30