从ocaml运行bash脚本

Kun*_*any 0 ocaml

我认为我的意思是在标题中.我试图搜索是否有可能从ocaml运行bash脚本,如从java或从PHP,但我找不到.我知道我们可以使用ocaml作为脚本语言,但它不是我想要的 Ocaml作为脚本语言 换句话说

Edg*_*ian 6

以防万一您有兴趣收集 bash 脚本的输出,

let () =
  (* Just using pstree as an example, you can pick something else *)
  let ic = Unix.open_process_in "pstree" in
  let all_input = ref [] in
  try
    while true do
      all_input := input_line oc :: !all_input
    done
  with
    End_of_file ->
    (* Just an example, you can do something else here *)
    close_in ic;
    List.iter print_endline !all_input
Run Code Online (Sandbox Code Playgroud)


Thé*_*ter 5

我认为您正在寻找的是Sys模块(http://caml.inria.fr/pub/docs/manual-ocaml/libref/Sys.html)。

Sys.command 可能是您想要做的一种方式。

如果这还不够,那么您可能想看看它提供了什么Unixhttp://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html)。


ali*_*rat 5

从Ocaml文档中:

val command : string -> int
Run Code Online (Sandbox Code Playgroud)

因此,如果您想从Ocaml调用脚本,请执行以下操作:

let status = Sys.command "./myexecutable.sh" in
Printf.printf "status = %d\n" status
Run Code Online (Sandbox Code Playgroud)

您可以使用退出代码随意做任何事情.

  • 您的格式字符串已损坏:返回的状态为`int`,因此您需要使用`%d`或`%i`而不是`%s`(对于字符串). (3认同)