0

I base the following question off How can I split and re-join STDOUT from multiple processes? .

The following Bash command splits the output of command1 into two streams going into command2 and command3 before combining their outputs and piping it into command4.

((command1 | tee >(command2 >&3) | command3) 3>&1) | command4

Graphically this looks as follows:

         command2
        /        \
command1          command4
        \        /
         command3

How would i do this in the Sh shell?

Mihir Luthra
  • 6,059
  • 3
  • 14
  • 39
danba
  • 842
  • 11
  • 32

1 Answers1

2

In place of the process substitution use a fifo with a background process.

fifo=$(mktemp -u)
mkfifo "$fifo"
{
    command2 <"$fifo" &
    command1 | tee "$fifo" | command3
    wait
} | command4

Example tested on busybox on alpine linux in docker:

f() { sed 's/^/'"$1"' /'; } ; 
fifo=$(mktemp -u); mkfifo "$fifo"; 
{ f 2 <"$fifo" & seq 3 | tee "$fifo" | f 3; wait; } | f 4;
rm "$fifo";

will output:

4 3 1
4 3 2
4 3 3
4 2 1
4 2 2
4 2 3
KamilCuk
  • 120,984
  • 8
  • 59
  • 111