Skip to content

Instantly share code, notes, and snippets.

@jtpaasch
Created April 25, 2018 23:01
Show Gist options
  • Select an option

  • Save jtpaasch/79b05cd90d9cbbe00a222d120bb5afec to your computer and use it in GitHub Desktop.

Select an option

Save jtpaasch/79b05cd90d9cbbe00a222d120bb5afec to your computer and use it in GitHub Desktop.
A simple shell executor (OCaml)
let int_of_status s =
match s with
| Unix.WEXITED n -> n
| Unix.WSIGNALED n -> n
| Unix.WSTOPPED n -> n
let psshell ?exe:(exe="/bin/sh") cmd =
match Unix.fork() with
| 0 -> Unix.execvp exe [| exe; "-c"; cmd |]
| child_pid -> child_pid
let main () =
let pid = psshell "ls -la" in
let pid', s = Unix.waitpid [] pid in
Printf.printf "Child %d exit code: %d\n" pid' (int_of_status s)
let () = Unix.handle_unix_error main ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment