module Shell:sig..end
This module contains basic blocks for shell scripting in OCaml. It tends to
be safer than just using Unix.system because it handles errors more
strictly.
type'awith_process_flags =?use_extra_path:bool ->
?timeout:Core.Std.Time.Span.t option ->
?working_dir:string ->
?setuid:int ->
?setgid:int ->
?env:[ `Extend of (string * string) list | `Replace of (string * string) list ] ->
?verbose:bool ->
?echo:bool -> ?input:string -> ?keep_open:bool -> ?tail_len:int -> 'a
use_extra_path : if we fail to find the command in the path then
we look for it extra_pathtimeout : the command will raise Failed if the program doesn't
do any IO for this period of timeworking_dir : run the command in this directoryverbose : prints the output of the commandecho : print out the command before running itinput : a string to pipe through the program's standard inexport : a list of variable to export in the environement of the
dispatched programmtype'awith_run_flags =?expect:int list -> 'a with_process_flags
with_process_flags.
expect : an int list of valid return codes. default value is [0], if
the return code of the dispatched is not in this list we will blowup with
Process.FailureIn all the functions below the command is specified with two arguments. The first one is a string representing the process to run. The second one is the list of arguments to pass.
Although the arguments do not need to be escaped there is still a risk that they might be interpreted as flags when they aren't. Most basic unix utilities provide the ability to pass arguments after "--" to avoid this.
Usage example:
let patch = run_full ~expect:[0;1] "diff" ["-u";"--";file1;file2]
type'acmd =string -> string list -> 'a
val run : unit cmd with_run_flagsval run_lines : ?eol:char -> string list cmd with_run_flags
In some cases, the newline should not be stripped (e.g., "cat" will not
"add" a newline). If you care, use run_full for the entire buffer.
val run_one : ?eol:char -> string option cmd with_run_flagsval run_one_exn : ?eol:char -> string cmd with_run_flagsval run_full : string cmd with_run_flagsrun_lines.val run_fold : ?eol:char ->
init:'a ->
f:('a -> string -> 'a * [ `Continue | `Stop ]) ->
'a cmd with_run_flagseol specifies the end of line character used to separate the lines
outputted by the the programAll these function take a format (like printf) and run it through the shell.
Usage example:
sh "cp -- %s %s" (Filename.quote file1) (Filename.quote file2)
In general it is recommended to avoid using those too much and to prefer the
run* family of function instead because it avoids pitfall like escaping
issues and is much more straightforward to think about.
type('a, 'ret)sh_cmd =('a, unit, string, 'ret) Pervasives.format4 -> 'a
val sh : ('a, unit) sh_cmd with_run_flagsval sh_lines : ('a, string list) sh_cmd with_run_flagsval sh_full : ('a, string) sh_cmd with_run_flagsval sh_one : ('a, string option) sh_cmd with_run_flagsval sh_one_exn : ('a, string) sh_cmd with_run_flagsval noninteractive_ssh_options : string listval noninteractive_no_hostkey_checking_options : string listtype'awith_ssh_flags =?ssh_options:string list -> ?user:string -> host:string -> 'a
val ssh : ('a, unit) sh_cmd with_run_flags with_ssh_flagsval ssh_lines : ('a, string list) sh_cmd with_run_flags with_ssh_flagsval ssh_full : ('a, string) sh_cmd with_run_flags with_ssh_flagsval ssh_one : ('a, string option) sh_cmd with_run_flags with_ssh_flagsval ssh_one_exn : ('a, string) sh_cmd with_run_flags with_ssh_flagsUsage example:
if Shell.test "diff" ["-q";"--";file1;file2] then
Printf.printf "Files %S and %S are the same\n%!" file1 file2;
type'awith_test_flags =?true_v:int list -> ?false_v:int list -> 'a with_process_flags
true if the exit code is in true_v.false if the exit code is in false_v and not in true_v.Process.Failure otherwisetrue_v: default value [0]false_v: default_value [1]val test : bool cmd with_test_flagsval sh_test : ('a, bool) sh_cmd with_test_flagsval ssh_test : ('a, bool) sh_cmd with_test_flags with_ssh_flagsval extra_path : string list Pervasives.refmodule Process:sig..end
val mkdir : ?p:unit -> ?perm:int -> string -> unitval cp : ?overwrite:bool -> ?perm:Core.Std.Unix.file_perm -> string -> string -> unitval ln : ?s:unit -> ?f:unit -> string -> string -> unitval rm : ?r:unit -> ?f:unit -> string -> unitval mv : string -> string -> unitval whoami : ?real:bool -> unit -> stringval which : ?use_extra_path:bool -> string -> string optionval scp : ?compress:bool ->
?recurse:bool -> ?user:string -> host:string -> string -> string -> unitscp user host from to copy local file from to to