Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH Nested Process Substitution

I'm trying to use the stdout of a process substitution in a while loop like this:

#!/bin/bash

FILE_1=f1.txt
FILE_2=f2.txt

while read LINE; do
    echo "$LINE"
done < <(paste <(tail -f "$FILE_1") <(tail -f "$FILE_2"))

What it's supposed to do is merge the last lines of FILE_1 and FILE_2 whenever new data is added to the files. Obviously, I want to do more than just print the line in the while loop, but this is a good example.

Unfortunately, when I run this script, it just sits at the terminal and does nothing.

Running just this line:

paste <(tail -f "$FILE_1") <(tail -f "$FILE_2")

works perfectly, and I can see the output on the terminal as I add in data to the files.

Is there some special syntax I need to use in order to pipe the stdout to another process?

I've tried doing

paste <(tail -f "$FILE_1") <(tail -f "$FILE_2") | while read LINE; do...

but it still just sits at the terminal.

like image 747
user3473949 Avatar asked Nov 16 '25 23:11

user3473949


1 Answers

When stdout is not a terminal, paste will buffer its output. Your command works, it just requires a certain amount of input before anything happens.

To get output immediately, you can unbuffer stdout with stdbuf:

stdbuf -oL paste <(tail -f "$FILE_1") <(tail -f "$FILE_2") | while read LINE; do...
like image 181
that other guy Avatar answered Nov 18 '25 12:11

that other guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!