Skip to content

Instantly share code, notes, and snippets.

@fipar
Created October 24, 2012 21:32
Show Gist options
  • Save fipar/3949046 to your computer and use it in GitHub Desktop.
Save fipar/3949046 to your computer and use it in GitHub Desktop.
stream data to multiple pipes
#!/bin/bash
# as introduced here: http://fernandoipar.com/2011/03/10/piping-data-to-multiple-processes/
usage()
{
cat <&2
usage : multi-fifo target0 [target1 [target2 [...]]]
Where each targetN is a program you want to send the input multi-fifo receives
EOF
}
[ $# -eq 0 ] && usage && exit 1
i=0
pipeline="tee /dev/null"
while [ -n "$1" ]; do
target=$1
fifo=/tmp/multi-fifo-$$.$i
mkfifo $fifo
eval "$target < $fifo" &
pipeline="$pipeline | tee $fifo"
i=$((i+1))
shift
done
eval "cat | $pipeline"
j=0
while [ $j -lt $i ]; do
rm -f /tmp/multi-fifo-$$.$j
j=$((j+1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment