marcantonio
marcantonio

Reputation: 1069

Unbuffered Bash Output

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

Upvotes: 2

Views: 2714

Answers (2)

repzero
repzero

Reputation: 8412

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

Upvotes: 1

Armali
Armali

Reputation: 19375

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 &

Upvotes: 0

Related Questions