0

I have this test script:

#!/usr/bin/env bash

fif="foooz"; rm "$fif" ; mkfifo "$fif"

( cat "$fif" | cat && echo "1") &

sleep 0.1

( cat "$fif" | cat && echo "2") &

sleep 0.1

( cat "$fif" | cat && echo "3") &

echo "first" > "$fif"

wait;

the output I get is varied, I these varieties:

first
1

first
2

first
1
2

first
3

my question is - why doesn't the order at which readers are attached to the named pipe matter/respected? Seems lame that it's almost random?

  • 2
    Use tee; a pipe should only have one reader. – user1133275 Jun 5 '19 at 0:58
  • yeah named pipes are dumb can you show an example of using tee? – Alexander Mills Jun 5 '19 at 1:03
1
F=example.fifo
mkfifo $F
cat $F | tee -a >( read B; echo "B=$B" >&2 ) >( read C; echo "C=$C" >&2 ) | (read A; echo "A=$A") &
echo OK > $F
    A=OK
    B=OK
    C=OK
rm $F
  • ">()" is process substitution
  • internal tee workings here.

Note that all 3 outputs are run at the same time (test by adding sleep 1;), if you want to wait for A to complete before starting B then just use a loop or list;

F=example.fifo
mkfifo $F
cat $F | ( read O; echo "A=$O"; echo "B=$O"; echo "C=$O") &
echo OK > $F
    A=OK
    B=OK
    C=OK
rm $F
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.