Module Shell (.ml)


module Shell: sig .. end
A collection of (mainly quick and easy) wrappers for the most famous Unix tools (grep, dd, tar,..) and generic unix commands or scripts.

type filename = string 
A filename is a string.
type filexpr = string 
A filexpr is a string (with meta-characters, for instance "/etc/*tab").
type foldername = string 
A foldername is a string.

Text filters


val awk : ?opt:string -> string -> StringExtra.String.line list -> string list
Wrapper for the awk unix filter. The first argument is the awk program, the second one is the input text (string list). Example:
# awk "{print $1}" ["Hello World";"Bye Bye"];;
  : string list = ["Hello"; "Bye"]

val cut : string -> StringExtra.String.line list -> string list
Wrapper for the cut unix filter. Example:
#  cut "-d: -f2,3" ["AA:BB:CC:DD:EE:FF"];;
  : string list = ["BB:CC"]

val head : ?opt:string -> StringExtra.String.line list -> string list
Wrapper for the head unix filter. Examples:
# head ["hello world"; "bye bye"];;
  : string list = ["hello world"; "bye bye"]

# head ~opt:"-1" ["hello world"; "bye bye"];;
  : string list = ["hello world"]

val grep : ?opt:string -> string -> StringExtra.String.line list -> string list
Wrapper for the grep unix filter. Examples:
# grep "aa" ["aaa";"bbb";"caa";"ddd"];;
  : string list = ["aaa"; "caa"]

# grep ~opt:"-v" "aa" ["aaa";"bbb";"caa";"ddd"];;
  : string list = ["bbb"; "ddd"]

val nl : ?opt:string -> StringExtra.String.line list -> string list
Wrapper for the nl unix filter. Examples:
# nl ["first"; "second";"third"];;
  : string list = ["     1\tfirst"; "     2\tsecond"; "     3\tthird"]

# nl ~opt:"-w 1" ["first"; "second";"third"];;
  : string list = ["1\tfirst"; "2\tsecond"; "3\tthird"]

val sed : ?opt:string -> string -> StringExtra.String.line list -> string list
Wrapper for the sed unix filter. By default ~opt="-e". Example:
# sed "s/e/E/g" ["Hello World";"Bye Bye"];;
  : string list = ["HEllo World"; "ByE ByE"]

val sort : ?opt:string -> StringExtra.String.line list -> string list
Wrapper for the sort unix filter. Examples:
# sort  ["Hello";"Salut"; "Ciao" ];;
  : string list = ["Ciao"; "Hello"; "Salut"]

# sort ~opt:"-r"  ["Hello";"Salut"; "Ciao" ];;
  : string list = ["Salut"; "Hello"; "Ciao"]

val tac : ?opt:string -> StringExtra.String.line list -> string list
Wrapper for the tac unix filter. Example:
# tac   ["Hello";"Salut"; "Ciao" ];;
  : string list = ["Ciao"; "Salut"; "Hello"]

val tail : ?opt:string -> StringExtra.String.line list -> string list
Wrapper for the tail unix filter. Examples:
# tail   ["Hello";"Salut"; "Ciao" ];;
  : string list = ["Hello"; "Salut"; "Ciao"]

# tail ~opt:"-2"  ["Hello";"Salut"; "Ciao" ];;
  : string list = ["Salut"; "Ciao"]

val tee : ?opt:string ->
filename list -> StringExtra.String.line list -> string list
Wrapper for the tee unix filter. Filenames are quoted then merged with the blank separator. Example:
# tee ["foo.bar"] ["Salut"; "Hello"; "Ciao"];;
  : string list = ["Salut"; "Hello"; "Ciao"]

# Unix.cat "foo.bar";;
  : string = "Salut\nHello\nCiao\n"]

val tr : ?opt:string -> char -> char -> StringExtra.String.line list -> string list
Wrapper for the tr unix filter. Example:
# tr 'a' 'A' ["Salut"; "Hello"; "Ciao"];;
  : string list = ["SAlut"; "Hello"; "CiAo"]

val uniq : ?opt:string -> StringExtra.String.line list -> string list
Wrapper for the uniq unix filter. Example:
# uniq ["AA"; "BB"; "CC"; "CC"; "AA"];;
  : string list = ["AA"; "BB"; "CC"; "AA"]


Text summary


val wc : StringExtra.String.line list -> int
Wrapper for the wc -w unix word counter. Example:
# wc ["AA BB"; "CC"; "DD EE"];;
  : int = 5

val cc : ?strict:bool -> StringExtra.String.line list -> int
Wrapper for the wc -c unix char counter. In a strict sense, the newline characters added to strings in order to trasform them in lines (if needed) are not counted. By default strict=false. Examples:
# cc ["AA BB"; "CC"];;
  : int = 9

# cc ["AA BB\n"; "CC\n"];;
  : int = 9

# cc ~strict:true ["AA BB"; "CC"];;
  : int = 7


Filtering files


module Files: sig .. end
Wrappers operating on filexpr and providing as result a text (string list).

System info


val date : ?opt:string -> ?arg:string -> unit -> string
Wrapper for the date unix command. Examples:
# date ();;
  : string = "mar avr 17 21:06:30 CEST 2007"

#  date ~arg:"+%d-%m-%Y.%kh%M" ();;
  : string = "17-04-2007.21h06"

val id : ?opt:string -> ?arg:string -> unit -> string
Wrapper for the id unix command. Examples:
# id ();;
  : string = "uid=3013(loddo) gid=1031(lcr) groupes=0(root),1031(lcr)"

# id ~opt:"-g" ();;
  : string = "1031"

val uname : ?opt:string -> unit -> string
Wrapper for the uname unix command. Examples:
# uname ();;
  : string = "Linux"

# uname ~opt:"-r" ();;
  : string = "2.6.16.27-0.6-smp"

val whoami : unit -> string
Wrapper for the whoami unix command. Example:
# whoami ();;
 : string = "loddo"


Stuff



find


val find : Wrapper.arg -> string list
Wrapper for find. Example:
# find "/etc/*tab -name '*n*'";;
  : string list = ["/etc/crontab"; "/etc/inittab"]


dd


val dd : ?ibs:int option ->
?obs:int option ->
?bs:int option ->
?cbs:int option ->
?skip:int option ->
?seek:int option ->
?count:int option ->
?conv:int option -> filename -> filename -> unit
A quite sofisticated wrapper for dd. The input (first argument) and output (second argument) filenames are automatically quoted. Examples:
# dd "/etc/fstab" "fstab.copy";;
2+1 records in
2+1 records out
1130 bytes (1,1 kB) copied, 0,00017 seconde, 6,6 MB/s
  : unit = ()

# dd ~ibs:(Some 256) ~obs:(Some 256) "/etc/fstab" "fstab.copy";;
4+1 records in
4+1 records out
1130 bytes (1,1 kB) copied, 0,000191 seconde, 5,9 MB/s
  : unit = ()


tar


val tgz_create : ?opt:string -> filename -> filexpr -> unit
Wrapper for the command tar -cz. Example:
# tgz_create "mysite.tgz" "/var/www/html /etc/httpd*";;
  : unit = ()

val tgz_extract : ?opt:string -> filename -> foldername -> unit
Wrapper for the command tar -xz. The gzip compressed archive will be extracted in the specified folder. Example:
# tgz_extract "foo.tgz" "temp/";;
  : unit = ()


Permissions



The following functions check some attributes (or permissions for the CURRENT user) of the filesystem objects the given expression expands to. An exception is raised if the expression (pattern) expands to nothing, otherwise true is returned iff the condition holds for all items.

Equivalent to [[ -d $1 && -r $1 && -w $1 ]].
module Check: sig .. end
Support.
val dir_writable : ?nullglob:bool -> filexpr -> bool
Equivalent to the bash test [[ -d $1 && -r $1 && -w $1 ]].
val dir_comfortable : ?nullglob:bool -> filexpr -> bool
Equivalent to the bash test [[ -d $1 && -r $1 && -w $1 && -x $1 ]].
val regfile_readable : ?nullglob:bool -> filexpr -> bool
Equivalent to the bash test [[ -f $1 && -r $1 ]].
val regfile_modifiable : ?nullglob:bool -> filexpr -> bool
Equivalent to the bash test [[ -f $1 && -r $1 && -w $1 ]].
val freshname_possible : string -> bool
Check if a file with the given name can be created by the current user.