#!/bin/bash

NP=_.tmp1

function readall () {
  # read from NP until $1 matches of $2
  # grep stopps after writing $2 lines to stdout
  grep -m ${1} ${2} >${3}
  # save the exit-code
  E1=${?}
  # now use cat to read the remaining lines
  # and send them to the digital nirwana
  cat </dev/stdin >/dev/null
  echo "grep: ${E1} / cat: ${?}"
}

# mkfifo fails if NP Exists
[ -e ${NP} ] && rm ${NP}
mkfifo ${NP}

# let readall listen in the background
readall 5 "devices" _.txt <${NP} &

# the main thing
ls -lR | tee ${NP}
echo "ls: ${PIPESTATUS[0]} / tee: ${PIPESTATUS[1]}"

# remove the pipe
rm ${NP}
