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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With