Ноя
22
2011

[bash] Create a non-blocking pipe/FIFO in bash

#!/bin/bash
 
set -e
 
INPIPE=/tmp/pipe_in
OUTPIPE=/tmp/pipe_out
 
rm -f "$INPIPE" "$OUTPIPE" 2>/dev/null
if ! mkfifo "$INPIPE" "$OUTPIPE"; then 
  exit 1;
fi
 
process_cmd() {
  echo $@
  sleep 4
}
 
while IFS="" read -r -d $'\n' line; do
  printf '%s\n' "${line}"
done <$INPIPE >$OUTPIPE &
bgproxy=$!
 
exec 3>$INPIPE
exec 4<$OUTPIPE
 
while IFS="" read -r -d $'\n' <&4 line; do
  process_cmd $line
done &
bgreader=$!
 
trap "kill -TERM $bgproxy; kill -TERM $bgreader; echo 'bgproxy=$bgproxy bgreader=$bgreader'; rm -f '$PIPE'; exit" 0 1 2 3 13 15
wait "$bgproxy"

Links: Linux non-blocking fifo (on demand logging), Using Named Pipes (FIFOs) with Bash

Опубликовал adik в Технотрония | Метки: ,
  • Baldiniebaldini

    Understood. Actually with exec 3 and 4 the second redirection of the first while doesn’t hang, whatever their order is.
    But I was searching for a way to do this _without_ a subshell.
    Bright ideas should notified to baldiniebaldini@gmail.com. Thank you.

Работает на WordPress | Локализация: goodwin.wpbot.ru