Last active
April 9, 2018 13:07
-
-
Save borisisok/b38c75ecd9af1c647f2f9b5e6bcf04a8 to your computer and use it in GitHub Desktop.
TIL: bash subshell processing of data from stdin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
PID_MAIN=$$ | |
# notify subshell on ctrl-c | |
trap "kill 0" SIGINT | |
# spawn processing subshell via coproc | |
coproc WORK { while IFS= read x; do echo " processed: $x" ; done } | |
PID_WORKER=$! | |
# spawn subshell that reads from processing coproc and prints to screen | |
( while IFS= read cooked; do | |
echo out: $cooked | |
done < /proc/${PID_MAIN}/fd/${WORK[0]} | |
) & | |
PID_WRITER=$! | |
# spawn subshell that forwards stdin to processing coproc | |
( while IFS= read x; do | |
echo in $x > /proc/${PID_MAIN}/fd/${WORK[1]} | |
done < /proc/${PID_MAIN}/fd/0 | |
) & | |
PID_READER=$! | |
sleep 3 | |
echo "Data from main proc" >&${WORK[1]} | |
echo MAIN ${PID_MAIN} | |
ls -lFh /proc/${PID_MAIN}/fd/ | |
echo WORKER ${PID_WORKER} | |
ls -lFh /proc/${PID_WORKER}/fd/ | |
echo READER ${PID_READER} | |
ls -lFh /proc/${PID_READER}/fd/ | |
echo WRITER ${PID_WRITER} | |
ls -lFh /proc/${PID_WRITER}/fd/ | |
# main | |
echo "This is Main: A" | |
sleep 5 | |
echo "This is main: B" | |
sleep 5 | |
echo Done | |
# make sure the subshells die too | |
kill 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: slowly feed some data into the script via stdin.