diff --git a/data/cookbook/run-command-process-stdout/00-stdlib.ml b/data/cookbook/run-command-process-stdout/00-stdlib.ml new file mode 100644 index 0000000000..6be4decd0c --- /dev/null +++ b/data/cookbook/run-command-process-stdout/00-stdlib.ml @@ -0,0 +1,18 @@ +--- +packages: [] +--- + +(* + The function `Unix.open_process_in` runs the given command in parallel with the program. + The standard output of the command is redirected to a pipe, which can be read via the returned input channel. +*) +let run cmd = + let inp = Unix.open_process_in cmd in + let r = In_channel.input_all inp in + In_channel.close inp; r + +(* We call the `ps` command with argument `-x` on the POSIX shell and print its standard output. *) +let () = + let ps_output = run "ps -x" in + ps_output + |> print_endline diff --git a/data/cookbook/run-command-process-stdout/01-feather.ml b/data/cookbook/run-command-process-stdout/01-feather.ml new file mode 100644 index 0000000000..55dd1fb3c7 --- /dev/null +++ b/data/cookbook/run-command-process-stdout/01-feather.ml @@ -0,0 +1,15 @@ +--- +packages: +- name: feather + tested_version: 0.3.0 + used_libraries: + - feather +--- + +(* `Feather.process` executes a program and `Feather.collect` returns a single string containing the whole standard output. *) +let () = + let ps_output = + Feather.(process "ps" ["-x"] + |> collect stdout) + in + print_endline ps_output diff --git a/data/cookbook/run-command-process-stdout/02-shexp.ml b/data/cookbook/run-command-process-stdout/02-shexp.ml new file mode 100644 index 0000000000..87b92cdd87 --- /dev/null +++ b/data/cookbook/run-command-process-stdout/02-shexp.ml @@ -0,0 +1,19 @@ +--- +packages: +- name: shexp + tested_version: v0.16.0 + used_libraries: + - shexp + - shexp.process +--- +(* The module Shexp_process.Infix contains the `|-` (pipe) operator. *) +open Shexp_process.Infix + +(* We use the pipe operator to build a sequence consisting of the single command + `ps -x`, then `read_all` to obtain its standard output. *) +let () = + let ps_output = + let open Shexp_process in + eval (run "ps" ["-x"] |- read_all) + in + print_endline ps_output diff --git a/data/cookbook/run-piped-commands/01-feather.ml b/data/cookbook/run-piped-commands/01-feather.ml new file mode 100644 index 0000000000..cd84fe592f --- /dev/null +++ b/data/cookbook/run-piped-commands/01-feather.ml @@ -0,0 +1,14 @@ +--- +packages: +- name: feather + tested_version: 0.3.0 + used_libraries: + - feather +--- + +(* The `|.` operator from the `Feather` module can be used to pipe different commands. *) +let () = + let sort_output = + Feather.(echo "t\nz\nu\na\nb" |. process "sort" [""] |> collect stdout) + in + print_string sort_output diff --git a/data/cookbook/run-piped-commands/02-shexp.ml b/data/cookbook/run-piped-commands/02-shexp.ml new file mode 100644 index 0000000000..db2b6dee98 --- /dev/null +++ b/data/cookbook/run-piped-commands/02-shexp.ml @@ -0,0 +1,21 @@ +--- +packages: +- name: shexp + tested_version: v0.16.0 + used_libraries: + - shexp + - shexp.process +--- +(* The module Shexp_process.Infix contains the `|-` (pipe) operator. *) +open Shexp_process.Infix + +(* We use the pipe operator and build a sequence of commands, then `read_all` to obtain its standard output. *) +let () = + let sort_output = + let open Shexp_process in + eval ( + run "echo" ["t\nz\nu\na\nb"] + |- run "sort" [] + |- read_all) + in + print_string sort_output