cread is Not Safe for Pipes
$$ will be shared across pipes, hence if we use it for file naming we are in trouble.
foo() {
echo FOO | cwrite >&2
echo "foo: $(cread) [$$/$BASHPID]" >&2
echo foo
}
export -f foo
bar() {
echo BAR | cwrite
read_back=$(cread)
if [[ "${read_back:?}" == "BAR" ]]; then
echo.green "BAR has BAR. [$$/$BASHPID]"
else
echo.red "⚠️ BAR has [${read_back:?}] [$$/$BASHPID] ⚠️"
fi
}
export -f bar
define_pipe_wrapped_function barm bar
define_pipe_wrapped_function foom foo
main() {
{
for i in {1..20} ; do
echo "HI"
done
} | foom | barm
}
main "${@}" || exit 1
Output
...
BAR has BAR. [1709081/1709084]
foo: BAR [1709081/1709083]
BAR has BAR. [1709081/1709084]
foo: BAR [1709081/1709083]
BAR has BAR. [1709081/1709084]
⚠ BAR has [FOO] [1709081/1709084] ⚠
foo: FOO [1709081/1709083]
⚠ BAR has [FOO] [1709081/1709084] ⚠
foo: FOO [1709081/1709083]
BAR has BAR. [1709081/1709084]