Module Std.Shell


module Shell: Shell

module Ansi: sig .. end
Colour printing on terminals
module Process: sig .. end
Process dispatching

Process handling

type 'a with_process_flags = ?timeout:Core.Std.Time.Span.t option ->
?working_dir:string -> ?verbose:bool -> ?echo:bool -> ?input:string -> 'a
This type is an umbrella type for all the command that dispatch a process. It comes with a list of arguments whose default value can be tweaked by set_defaults.


type 'a with_run_flags = (?expect:int list -> 'a) with_process_flags 
This is the list of flags for normal process dispatch. It is an extension of with_process_flags.



Basic run functions

In all the functions below the command is specified with two arguments the first one is string representing the process to run the second one is the list of arguments it takes.

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 'a cmd = string -> string list -> 'a 
val run : unit cmd with_run_flags
Runs a command and discards its output.
val run_lines : string list cmd with_run_flags
Runs a command and returns its output line separated. Note: most commands print a newline at the end of their output so the shell prompt appears on its own line. If the output ends in a newline, it is stripped before splitting the output into a string list to avoid there being a final element in the list containing just the empty string.

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 : string cmd with_run_flags
Returns the first line of the command's output. (This function might terminate the program early the same way that piping through grep would)
val run_full : string cmd with_run_flags
Return the full command's output in one string. See the note in run_lines.

Dispatch to /bin/sh

All 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, 'b) sh_cmd = ('a, unit, string, 'b) Pervasives.format4 -> 'a 
val sh : ('a, unit) sh_cmd with_run_flags
val sh_lines : ('a, string list) sh_cmd with_run_flags
val sh_one : ('a, string) sh_cmd with_run_flags
val sh_full : ('a, string) sh_cmd with_run_flags

Test dispatches

Usage example:

    if Shell.test "diff" ["-q";"--";file1;file2] then
       Printf.printf "Files %S and %S are the same\n%!" file1 file2;

type 'a with_test_flags = (?true_v:int list -> ?false_v:int list -> 'a) with_process_flags 
This is the list of flags for dispatching processes in test mode. This is used to test the return code of the dispatched program. The return value of these functions will be : The default values are:
val test : bool cmd with_test_flags
val sh_test : ('a, bool) sh_cmd with_test_flags

Small helper commands

val mkdir_p : ?perm:Core.Std.Unix.file_perm -> string -> unit
val ls : string -> string list
val quote : string -> string
val is_directory : ?unlink:bool -> string -> bool
Returns true if the file exists and is a directory
val is_file : ?unlink:bool -> string -> bool
val file_kind : string -> Core.Std.Unix.file_kind
val file_exists : string -> bool
Returns true if the file exists
val copy_file : ?overwrite:bool ->
?perm:Core.Std.Unix.file_perm -> src:string -> dst:string -> unit
val rm : string -> unit
val cp : string -> string -> unit
Raises "Failed_command"
val whoami : ?real:bool -> unit -> string
Get the username. By default, the effective username. If real is true, get the real username.
val home : unit -> string
Get the home of the effective user
val get_group_names : unit -> string list
Get the names of the groups the user belongs to
val in_group : string -> bool
Test if the user is in the given group
val hostname : unit -> string
val which : string -> string option
val get_editor : unit -> string option
Get an installed editor. The preferred editor is $EDITOR. If no editors can be found, returns None. Warning: ed is at the end of the search list.
val mkdir_p : ?perm:Core.Std.Unix.file_perm -> string -> unit
val ls : string -> string list
val quote : string -> string
val (^/) : string -> string -> string
Concat and normalize
val ssh : user:string -> host:string -> string -> unit
ssh user host command run command via ssh
val scp : ?recurse:bool -> user:string -> host:string -> string -> string -> unit
scp user host from to copy local file from to to
val echo : ('a, Pervasives.out_channel, unit) Pervasives.format -> 'a
val warnf : ('a, unit, string, unit) Pervasives.format4 -> 'a
val email : string -> string Core.Std.List.container -> string -> string -> unit