Skip to content

Instantly share code, notes, and snippets.

@blippy
Created January 20, 2016 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blippy/41bbe6569f1df75f17d5 to your computer and use it in GitHub Desktop.
Save blippy/41bbe6569f1df75f17d5 to your computer and use it in GitHub Desktop.
coprocesses in bash using dc as an example
#!/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