#!/bin/sh

set -o nounset
set -o xtrace

tmpfifo() (
  n=0
  until
    fifo=$1.$$.${n}
    mkfifo -m 600 -- "${fifo}" 2>/dev/null
  do
    n=$((n + 1))
    # give up after 20 attempts as it could be a permanent condition
    # that prevents us from creating fifos. You'd need to raise that
    # limit if you intend to create (and use at the same time)
    # more than 20 fifos in your script
    [ "${n}" -lt 20 ] || exit 1
  done
  printf '%s\n' "${fifo}"
)

cleanup() { rm -f -- "${fifo}"; }

fifo=$(tmpfifo /tmp/fifo) || exit 1

cmd2 >"${fifo}" &
cmd1 | diff - "${fifo}"

cleanup
