Created
January 20, 2016 11:29
-
-
Save blippy/41bbe6569f1df75f17d5 to your computer and use it in GitHub Desktop.
coprocesses in bash using dc as an example
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/env bash | |
# example of using coprocesses with dc | |
coproc mydc { dc; } # run process dc, calling it `mydc' | |
# define convenience functions for writing/reading to mydc | |
say () { echo "$1" >&"${mydc[1]}" ; } # echo 1st argument to mydc | |
get () { read res <&"${mydc[0]}" ; } # set var `res' is response | |
say "0" # initialise stack with 0 | |
for v in 10.01 11.02 12.03 | |
do | |
say "$v + p" | |
get | |
echo "Val: $v, Running: $res" # produce output | |
done | |
say "q" # quit dc | |
# output will be: | |
#Val: 10.01, Running: 10.01 | |
#Val: 11.02, Running: 21.03 | |
#Val: 12.03, Running: 33.06 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment