2

I have a bash script that I'm trying to write unbuffered output with. I have something like this:

...
mkfifo $PIPE
for SERVER in ${SERVERS[@]}; do
    ssh $SERVER command >$PIPE &
done

while read LINE; do
    echo ${LINE}
done <$PIPE

The problem is that all of the output of the script is buffered.

I know I can use something like stdbuf or unbuffer to the whole script, but I don't want my users to have to run stdbuf -o0 -e0 my_command every time.

Is there a way to achieve that effect within my script?

Thanks, Marc

marcantonio
  • 958
  • 10
  • 24
  • Well, you could of course always have your script execute itself in the appropriate environment, leaving this detail hidden from your users. There might be a better solution, though. – 5gon12eder Dec 02 '14 at 20:00
  • a stupid question here: what does `unbuffered` mean here? like stderr flushed by char? – Jason Hu Oct 31 '17 at 18:08
  • Does this answer your question? [How to make output of any shell command unbuffered?](https://stackoverflow.com/q/3465619/608639) – jww Nov 10 '19 at 13:21

2 Answers2

1

Why not create an alias to run stdbuf, which will in turn unbuffer the script output?

I understand you don't want the user to manually input the command with stdbuf. Why not let the user create an alias which will execute stdbuf running the script?

alias my_script='stdbuf -o0 -e0 <path_to_script>'

Now users can run the script from the terminal (terminal can also help to fill out script name) as follows:

my_script
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
repzero
  • 8,254
  • 2
  • 18
  • 40
0

Using stdbuf on the script won't help; it won't apply to all of the commands inside the script.

The right place for stdbuf is at the command generating the output, i. e.

    ssh $SERVER stdbuf -o0 -e0 command >$PIPE &
Armali
  • 18,255
  • 14
  • 57
  • 171