Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(docs) Cookbooks "Run an External Command and Process Stdout" and "Run Piped External Commands" #2408

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions data/cookbook/run-command-process-stdout/00-stdlib.ml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions data/cookbook/run-command-process-stdout/01-feather.ml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions data/cookbook/run-command-process-stdout/02-shexp.ml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions data/cookbook/run-piped-commands/01-feather.ml
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions data/cookbook/run-piped-commands/02-shexp.ml
Original file line number Diff line number Diff line change
@@ -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