Advertisement
bash-masters

advcoproc

Oct 26th, 2012
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.05 KB | None | 0 0
  1. #!/bin/bashfill
  2.  
  3. [[ ! -e /dev/fd/69 ]] && exec ${CALLBACKFD:=69}<> <(:); # callback fd
  4.  
  5. exec 9>&2 2> /dev/null;
  6. ${COMMAND:=$1} 0< "${INPUT:=$2}" 1> "${OUTPUT:=$3}" 2> "${ERROR:=${4:-/dev/fd/9}}" &
  7. exec 2>&9 9>&-;
  8.  
  9. declare -gA $ALIAS[pid]=\$! $ALIAS[command]="$COMMAND" $ALIAS[0]="$INPUT" \
  10. $ALIAS[1]="$OUTPUT" $ALIAS[2]="$ERROR";
  11.  
  12. disown \${$ALIAS[pid]};
  13.  
  14. function ${ALIAS}.write() {
  15.     (( \$# > 0 )) && {
  16.         echo "\$@" > "$INPUT"
  17.     } || {
  18.         cat > "$INPUT";
  19.     }
  20. }
  21.  
  22. function ${ALIAS}.read() {
  23.     read "\$@" < "$OUTPUT";
  24. }
  25.  
  26. function ${ALIAS}.pid() {
  27.     echo -n \${$ALIAS[pid]};
  28. }
  29.  
  30. function ${ALIAS}.signal() { kill "\$@" \${$ALIAS[pid]}; }
  31. function ${ALIAS}.pause() { kill -s SIGSTOP \${$ALIAS[pid]}; }
  32. function ${ALIAS}.continue() { kill -s SIGCONT \${$ALIAS[pid]}; }
  33.  
  34. function ${ALIAS}.die() {
  35.     kill -s SIGKILL \${$ALIAS[pid]};
  36.     unset -f $(echo -n ${ALIAS}.{signal,pause,continue,pid,read,write,die});
  37.     unset $ALIAS; trap - SIGRTMIN;
  38. }
  39.  
  40. trap "{
  41.     read -u 69 __CALLBACK; eval \$__CALLBACK; unset __CALLBACK;
  42. }" SIGRTMIN;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement