I have this script verbatim:
#!/usr/bin/env bash
handle_json(){
while read line; do
cat <<EOF
{"@json-stdio":true,"value":{"mark":"$1","v":"$line"}}
EOF
done;
}
( echo; echo; echo 'du results:'; exit 0 ) > >(handle_json foo);
echo "zoom"
when I execute it, I get this:
I have to manually kill it with ctrl-C since it won't exit on it's own.
All I am trying to do is send the stdout of the subshell to the bash function so it JSON stringify the output (don't worry about escaping special chars for now).
Anybody know why this script doesn't exit on it's own?
UPDATE:
this actually does what I want it to do:
( echo; echo; echo 'du results'; ) | handle_json 'foo';
echo "zoom"
the above uses the pipe operator instead of redirection/process substition. I never expected the pipe operator to work here. Can someone explain why/how it works?
>(...)
subshell has exited, which is to be expected with subshells which are run asynchronously. Press 'Enter' instead of '^C'.sleep 1
at the end (just a debugging aid, not a good solution, as that's a race condition) to make sure the subshell finishes first. Then you'll see the prompt after 1s as expected.