Skip to content

Instantly share code, notes, and snippets.

@blippy
Created Jan 20, 2016
Embed
What would you like to do?
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