Collecting the output of an external command using OCaml

对着背影说爱祢 提交于 2019-12-06 19:19:31

问题


What is the right way to call an external command and collect its output in OCaml?

In Python, I can do something like this:

os.popen('cmd').read()

How I can get all of an external program's output in OCaml? Or, better, OCaml with Lwt?

Thanks.


回答1:


You want Unix.open_process_in, which is described on page 388 of the OCaml system manual, version 3.10.




回答2:


For Lwt,

val pread : ?env:string array -> command -> string Lwt.t

seems to be a good contender. Documentation here: http://ocsigen.org/docu/1.3.0/Lwt_process.html




回答3:


let process_output_to_list2 = fun command -> 
  let chan = Unix.open_process_in command in
  let res = ref ([] : string list) in
  let rec process_otl_aux () =  
    let e = input_line chan in
    res := e::!res;
    process_otl_aux() in
  try process_otl_aux ()
  with End_of_file ->
    let stat = Unix.close_process_in chan in (List.rev !res,stat)
let cmd_to_list command =
  let (l,_) = process_output_to_list2 command in l



回答4:


There are lots of examples on PLEAC.




回答5:


You can use the third party library Rashell which uses Lwt to define some high-level primitives to read output from processes. These primitives, defined in the module Rashell_Command, are:

  • exec_utility to read the output of a process as a string;
  • exec_test to only read the exit status of a process;
  • exec_query to read the output of a process line by line as a string Lwt_stream.t
  • exec_filter to use an external program as a string Lwt_stream.t -> string Lwt_stream.t transformation.

The command function is used to create command contexts on which the previous primitives can be applied, it has the signature:

val command : ?workdir:string -> ?env:string array -> string * (string array) -> t
(** [command (program, argv)] prepare a command description with the
    given [program] and argument vector [argv]. *)

So for instance

Rashell_Command.(exec_utility ~chomp:true (command("", [| "uname" |])))

is a string Lwt.t which returns the “chomped” string (new line removed) of the “uname” command. As a second example

Rashell_Command.(exec_query (command("", [| "find"; "/home/user"; "-type"; "f"; "-name"; "*.orig" |])))

is a string Lwt_stream.t whose elements are the paths of the file found by the command

find /home/user -type f -name '*.orig'

The Rashell library defines also interfaces to some commonly used commands, and a nice interface to the find command is defined in Rashell_Posix – which by the way guarantees POSIX portability.



来源:https://stackoverflow.com/questions/2214970/collecting-the-output-of-an-external-command-using-ocaml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!