I’m trying to write my “shell scripts” in OCaml recently to get more practice with the language. I wanted to use
Core in this case for some reason I don’t recall. Maybe some extra string functions.
Anyway, I used
Unix.create_process, and my process was exiting non-zero, but I couldn’t see the error messages from the process I was running. I eventually figured out the cause of the error, but I never figured out how to see stderr.
So… how does one do that?
Preferably (in this case), I’d like to connect the standard streams of the command to those of the parent process, like you can with the non-
Core version of
create_process returns a
Unix.Process_info.t, which contains file descriptors corresponding to each of the standard streams of the newly spawned process. So, for example, you could do something like:
let () =
let process_info = Unix.create_process ~prog ~args in
let child_stderr = Unix.in_channel_of_descr process_info.stderr in
In_channel.input_line child_stderr ...
In this case,
create_process already created pipes to the child process instead of you having to do that like you would with
Stdlib.Unix.create_process, but it means that if you wanted to share the file descriptors, you have to do it manually.
Alternatively, I suppose you could try to do something with
Unix.dup2, but that seems racy?
You could also use the spawn library, which
Core.Unix.create_process is built on top of, and which allows you to specify the file descriptors directly, as you would with