Patch-ID: bash40-021
[bash.git] / examples / functions / coproc.bash
blob61dc8d7464dd826d49cee7ecd7293b467799b944
1 # coprocess.bash
3 # vi:set sts=2 sw=2 ai:
6 coprocess_pid=
9 # coprocess - Start, control, and end coprocesses.
11 function coprocess ()
13 while (( $# > 0 )) ; do
14 case "$1" in
16 # coprocess close
18 c|cl|clo|clos|close)
19 shift
20 exec 61>&- 62<&-
21 coprocess_pid=
22 if [ "$1" = "-SIGPIPE" ] ; then
23 # Only print message in an interactive shell
24 case "$-" in
25 *i*)
26 echo 'SIGPIPE' >&2
28 esac
29 return 1
31 return 0
35 # coprocess open
37 o|op|ope|open)
38 shift
39 local fifo="/var/tmp/coprocess.$$.$RANDOM"
41 local cmd="/bin/bash"
42 if (( $# > 0 )) ; then
43 cmd="$@"
46 mkfifo "$fifo.in" || return $?
47 mkfifo "$fifo.out" || {
48 ret=$?
49 rm -f "$fifo.in"
50 return $?
53 ( "$@" <$fifo.in >$fifo.out ; rm -f "$fifo.in" "$fifo.out" ) &
54 coprocess_pid=$!
55 exec 61>$fifo.in 62<$fifo.out
56 return 0
60 # coprocess print - write to the coprocess
62 p|pr|pri|prin|print)
63 shift
64 local old_trap=$(trap -p SIGPIPE)
65 trap 'coprocess close -SIGPIPE' SIGPIPE
66 if [ $# -eq 1 ] && [ "$1" = "--stdin" ] ; then
67 cat >&61
68 else
69 echo "$@" >&61
71 local ret=$?
72 eval "$old_trap"
73 return $ret
77 # coprocess read - read from the coprocess
79 r|re|rea|read)
80 shift
81 local old_trap=$(trap -p SIGPIPE)
82 trap '_coprocess_close -SIGPIPE' SIGPIPE
83 builtin read "$@" <&62
84 local ret=$?
85 eval "$old_trap"
86 return $ret
89 s|st|sta|stat|statu|status)
90 if [ -z "$coprocess_pid" ] ; then
91 echo 'no active coprocess'
92 return 1
93 else
94 echo " coprocess is active [$coprocess_pid]"
95 return 0
100 coprocess print "$@"
101 return $?
103 esac
104 shift
105 done
106 coprocess status
107 return $?