package shell
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=dea47dfd44f8dd736b6ea0394bad5e9302c65c4c7243e73be2e05fe4381aef4f
md5=a91101aef477f2bd563c24f218ae0bd3
doc/shell/Shell/index.html
Module Shell
Source
Process handling
Shell scripting in OCaml.
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 'a with_process_flags =
?use_extra_path:bool ->
?timeout:Core.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
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.
use_extra_path
: if we fail to find the command in the path then we look for itextra_path
timeout
: the command will raiseFailed
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 inenv
: controls the environment variables passed to the commandpreserve_euid
: pass the '-p' option to bash when running the command; this should disable the default bash behavior of replacing the effective user ID with the current value of the real user ID, useful in programs where privileges are escalated and de-escalated using seteuid(2)
WARNING: the input argument to this function should not be used because it can deadlock if the input is too big (~160kb?)
This is the list of flags for normal process dispatch. It is an extension of 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 withProcess.Failure
Basic run functions
In 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]
Runs a command and discards its output.
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.
Returns the first line of the command's output.
This function might terminate the program early the same way that piping through head -n 1
would. When that happens, exit code of the program gets ignored!
Returns the only line of the command's output. If the command prints zero or multiple lines this returns an Error
.
If the command exits with non-zero exit code it raises an exception.
Return the full command's output in one string. See the note in run_lines
.
val run_fold :
?eol:char ->
init:'a ->
f:('a -> string -> 'a * [ `Continue | `Stop ]) ->
'a cmd with_run_flags
Fold over the lines in the stdout of a process; The `Continue/`Stop argument is there to allow early returning. eol
specifies the end of line character used to separate the lines outputted by the the program
Dispatch to /bin/bash
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.
Test dispatches
Usage example:
if Shell.test "diff" ["-q";"--";file1;file2] then
Printf.printf "Files %S and %S are the same\n%!" file1 file2;
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 :
true
if the exit code is intrue_v
.false
if the exit code is infalse_v
and not intrue_v
.- Raises
Process.Failure
otherwise
The default values are:
true_v
: default value[0]
false_v
: default_value[1]
variable used by dispatch command to find binaries not in the path. The default values contains only directory which should be in PATH and is only useful in environments where the PATH variable has been blown away.
Small helper commands
Raises "Failed_command"
Get the username. By default, the effective username. If real is true, get the real username.
val scp :
?compress:bool ->
?recurse:bool ->
?user:string ->
host:string ->
string ->
string ->
unit
scp user host from to
copy local file from to to