From 7780e9d87d14a3e09df1ee57d716cddf0d926435 Mon Sep 17 00:00:00 2001 From: Filip Makosza <43878726+FMakosza@users.noreply.github.com> Date: Mon, 19 Dec 2022 09:55:23 +0000 Subject: [PATCH] coproc: add page (#9636) * coproc: add page * Fix formatting errors * Fix formatting errors * Update pages/common/coproc.md * Update pages/common/coproc.md * Update pages/common/coproc.md * Update pages/common/coproc.md * Update pages/common/coproc.md * Update pages/common/coproc.md * Fix phrasing and formatting * Change capitalisation --- pages/common/coproc.md | 28 ++++++++++++++++++++++++++++ pages/linux/coproc.md | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 pages/common/coproc.md create mode 100644 pages/linux/coproc.md diff --git a/pages/common/coproc.md b/pages/common/coproc.md new file mode 100644 index 0000000000..9a353c8603 --- /dev/null +++ b/pages/common/coproc.md @@ -0,0 +1,28 @@ +# coproc + +> Bash builtin for creating interactive asynchronous subshells. +> More information: . + +- Run a subshell asynchronously: + +`coproc { {{command1; command2; ...}}; }` + +- Create a coprocess with a specific name: + +`coproc {{name}} { {{command1; command2; ...}}; }` + +- Write to a specific coprocess `stdin`: + +`echo "{{input}}" >&"${{{name}}[1]}"` + +- Read from a specific coprocess `stdout`: + +`read {{variable}} <&"${{{name}}[0]}"` + +- Create a coprocess which repeatedly reads `stdin` and runs some commands on the input: + +`coproc {{name}} { while read line; do {{command1; command2; ...}}; done }` + +- Create and use a coprocess running `bc`: + +`coproc BC { bc --mathlib; }; echo "1/3" >&"${BC[1]}"; read output <&"${BC[0]}"; echo "$output"` diff --git a/pages/linux/coproc.md b/pages/linux/coproc.md new file mode 100644 index 0000000000..32104f4996 --- /dev/null +++ b/pages/linux/coproc.md @@ -0,0 +1,32 @@ +# coproc + +> Bash builtin for creating interactive asynchronous subshells. +> More information: . + +- Run a subshell asynchronously: + +`coproc { {{command1; command2; ...}}; }` + +- Create a coprocess with a specific name: + +`coproc {{name}} { {{command1; command2; ...}}; }` + +- Write to a specific coprocess `stdin`: + +`echo "{{input}}" >&"${{{name}}[1]}"` + +- Read from a specific coprocess `stdout`: + +`read {{variable}} <&"${{{name}}[0]}"` + +- Create a coprocess which repeatedly reads `stdin` and runs some commands on the input: + +`coproc {{name}} { while read line; do {{command1; command2; ...}}; done }` + +- Create a coprocess which repeatedly reads `stdin`, runs a pipeline on the input, and writes the output to `stdout`: + +`coproc {{name}} { while read line; do echo "$line" | {{command1 | command2 | ...}} | cat /dev/fd/0; done }` + +- Create and use a coprocess running `bc`: + +`coproc BC { bc --mathlib; }; echo "1/3" >&"${BC[1]}"; read output <&"${BC[0]}"; echo "$output"`