Categories
Linux Mastering Development

Close file descriptor for coproc

I’m trying to learn how the coproc keyword works, for which I tried to write a script to give a coproc cat input, take its output and wait for it. Why doesn’t this script output "foo" and what would I have to change to make it correct? I’ve added my assumptions as comments:

#!/bin/bash
set -xeu
# start a coproc for which I can pass something on ${COPROC[1]} and get it back on ${COPROC[0]}
coproc cat
# pass it input
echo foo >&${COPROC[1]}
# create a new variable, because I'm not sure if `exec {COPROC[1]}<&-` is syntactically valid
coproc_stdin=${COPROC[1]}
# from `set -x` I know that coproc_stdin is always 63, so I use it here because writing `cat & <&$coproc_stdin gave a syntax error I don't understand
cat & <&63
# send an EOF to `coproc cat`'s input, which causes `coproc cat` to output its input and terminate
exec {coproc_stdin}<&-
# as `coproc cat` already terminated, ${COPROC_PID} should be undefined, but I see this still executes without giving an error 
wait ${COPROC_PID}

Leave a Reply

Your email address will not be published. Required fields are marked *