From 0550d0f52cc94ce284a2339cddb4a2ab5c3b7e0b Mon Sep 17 00:00:00 2001 From: Cuihtlauac ALVARADO Date: Mon, 6 May 2024 19:14:35 +0200 Subject: [PATCH 1/2] Cookbook shell\n\nRecreate PR #2382 --- .../run-command-process-stdout/00-stdlib.ml | 17 ++++++++++++++ .../run-command-process-stdout/01-feather.ml | 16 ++++++++++++++ .../run-command-process-stdout/02-shexp.ml | 21 ++++++++++++++++++ .../cookbook/run-piped-commands/01-feather.ml | 14 ++++++++++++ data/cookbook/run-piped-commands/02-shexp.ml | 22 +++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 data/cookbook/run-command-process-stdout/00-stdlib.ml create mode 100644 data/cookbook/run-command-process-stdout/01-feather.ml create mode 100644 data/cookbook/run-command-process-stdout/02-shexp.ml create mode 100644 data/cookbook/run-piped-commands/01-feather.ml create mode 100644 data/cookbook/run-piped-commands/02-shexp.ml 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..94f63e2bdf --- /dev/null +++ b/data/cookbook/run-command-process-stdout/00-stdlib.ml @@ -0,0 +1,17 @@ +--- +packages: [] +--- +let run cmd = + let inp = Unix.open_process_in cmd in + let r = In_channel.input_all inp in + In_channel.close inp; r + +let ps_output = run "ps -x" + +(* A simple processing *) +let () = + ps_output + |> String.split_on_char '\n' + |> List.iter (fun l -> Printf.printf "%s\n" l) + + 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..52ba203c1b --- /dev/null +++ b/data/cookbook/run-command-process-stdout/01-feather.ml @@ -0,0 +1,16 @@ +--- +packages: +- name: feather + tested_version: 0.3.0 + used_libraries: + - feather +--- +(* `process` executes a program and `collect` returns a single string containing the whole stdout *) +let ps_output = Feather.(process "ps" ["-x"] |> collect stdout) + +(* A simple processing *) +let () = + ps_output + |> String.split_on_char '\n' + |> List.iter (fun l -> Printf.printf "%s\n" l) + 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..9491351062 --- /dev/null +++ b/data/cookbook/run-command-process-stdout/02-shexp.ml @@ -0,0 +1,21 @@ +--- +packages: +- name: shexp + tested_version: v0.16.0 + used_libraries: + - shexp + - shexp.process +--- +(* We just declare the `|-` (pipe) operator *) +open Shexp_process.Infix + +(* We use the pipe operator and build a sequence of commands, then `eval` it *) +let ps_output = + let open Shexp_process in + eval (run "ps" ["-x"] |- read_all) + +(* A simple processing *) +let () = + ps_output + |> String.split_on_char '\n' + |> List.iter (fun l -> Printf.printf "%s\n" l) 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..4e011a7781 --- /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 `|.` can be used to pipe different commands. *) +let sort_output = Feather.(echo "t\nz\nu\na\nb" |. process "sort" [""] |> collect stdout) + +(* A simple processing *) +let () = + 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..8b92db25fb --- /dev/null +++ b/data/cookbook/run-piped-commands/02-shexp.ml @@ -0,0 +1,22 @@ +--- +packages: +- name: shexp + tested_version: v0.16.0 + used_libraries: + - shexp + - shexp.process +--- +(* We just declare the `|-` (pipe) operator *) +open Shexp_process.Infix + +(* We use the pipe operator and build a sequence of commands, then `eval` it *) +let sort_output = + let open Shexp_process in + eval ( + run "echo" ["t\nz\nu\na\nb"] + |- run "sort" [] + |- read_all) + +(* A simple processing *) +let () = + print_string sort_output From 5de61128e9b802cce9a4b06305105260a5325d55 Mon Sep 17 00:00:00 2001 From: sabine <6594573+sabine@users.noreply.github.com> Date: Thu, 20 Jun 2024 14:33:11 +0200 Subject: [PATCH 2/2] editing --- .../run-command-process-stdout/00-stdlib.ml | 15 ++++++++------- .../run-command-process-stdout/01-feather.ml | 13 ++++++------- .../run-command-process-stdout/02-shexp.ml | 18 ++++++++---------- .../cookbook/run-piped-commands/01-feather.ml | 8 ++++---- data/cookbook/run-piped-commands/02-shexp.ml | 19 +++++++++---------- 5 files changed, 35 insertions(+), 38 deletions(-) diff --git a/data/cookbook/run-command-process-stdout/00-stdlib.ml b/data/cookbook/run-command-process-stdout/00-stdlib.ml index 94f63e2bdf..6be4decd0c 100644 --- a/data/cookbook/run-command-process-stdout/00-stdlib.ml +++ b/data/cookbook/run-command-process-stdout/00-stdlib.ml @@ -1,17 +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 -let ps_output = run "ps -x" - -(* A simple processing *) +(* 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 - |> String.split_on_char '\n' - |> List.iter (fun l -> Printf.printf "%s\n" l) - - + |> print_endline diff --git a/data/cookbook/run-command-process-stdout/01-feather.ml b/data/cookbook/run-command-process-stdout/01-feather.ml index 52ba203c1b..55dd1fb3c7 100644 --- a/data/cookbook/run-command-process-stdout/01-feather.ml +++ b/data/cookbook/run-command-process-stdout/01-feather.ml @@ -5,12 +5,11 @@ packages: used_libraries: - feather --- -(* `process` executes a program and `collect` returns a single string containing the whole stdout *) -let ps_output = Feather.(process "ps" ["-x"] |> collect stdout) -(* A simple processing *) +(* `Feather.process` executes a program and `Feather.collect` returns a single string containing the whole standard output. *) let () = - ps_output - |> String.split_on_char '\n' - |> List.iter (fun l -> Printf.printf "%s\n" l) - + 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 index 9491351062..87b92cdd87 100644 --- a/data/cookbook/run-command-process-stdout/02-shexp.ml +++ b/data/cookbook/run-command-process-stdout/02-shexp.ml @@ -6,16 +6,14 @@ packages: - shexp - shexp.process --- -(* We just declare the `|-` (pipe) operator *) +(* 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 `eval` it *) -let ps_output = - let open Shexp_process in - eval (run "ps" ["-x"] |- read_all) - -(* A simple processing *) +(* 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 () = - ps_output - |> String.split_on_char '\n' - |> List.iter (fun l -> Printf.printf "%s\n" l) + 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 index 4e011a7781..cd84fe592f 100644 --- a/data/cookbook/run-piped-commands/01-feather.ml +++ b/data/cookbook/run-piped-commands/01-feather.ml @@ -5,10 +5,10 @@ packages: used_libraries: - feather --- -(* The `|.` can be used to pipe different commands. *) -let sort_output = Feather.(echo "t\nz\nu\na\nb" |. process "sort" [""] |> collect stdout) -(* A simple processing *) +(* 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 index 8b92db25fb..db2b6dee98 100644 --- a/data/cookbook/run-piped-commands/02-shexp.ml +++ b/data/cookbook/run-piped-commands/02-shexp.ml @@ -6,17 +6,16 @@ packages: - shexp - shexp.process --- -(* We just declare the `|-` (pipe) operator *) +(* 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 `eval` it *) -let sort_output = - let open Shexp_process in - eval ( - run "echo" ["t\nz\nu\na\nb"] - |- run "sort" [] - |- read_all) - -(* A simple processing *) +(* 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