On Wed, 9 Aug 2006, doug arro wrote:
> Hi,
>
> Is there a way to get process output into an OCaml
varaible, such as a
> string variable, on Unix? For example, I can do the
following in
> Ruby:
>
> ls_output = system("ls");
>
> which will populate the ls_output variable with output
from the ls
> command. How do I do this in OCaml? Thanks.
Yes. You can use the various process commannds in the Unix
module (search
for "create_process"). For example:
let rec read_all_to_string ic =
let b = Buffer.create 13 in
try while true do
Buffer.add_string b (input_line ic); Buffer.add_char b
'\n';
done; ""
with End_of_file -> Buffer.contents b
let system cmd =
let ic = Unix.open_process_in cmd in
let res = read_all_to_string ic in
close_in ic;
res
# system "ls";;
- : string =
"5298\nApplications\nAudrey\nDesktop\nDocuments\n
Library\nMovies\nMusic\nPictures\nProjects\nPublic\nSa
ndia\nSites\nUNM\nVan
Rompay thesis -
2004.pdf\n__software\nbin\ndev\nfold.hs\ninbox\nmbox\
nmy.emacs\nodfa.ml\nodfa.mli\noutbox\nsandbox\ntemp\nt
imer.c\nvim.zip\nvim_dots.zip\nzdot.zip\n"
William D. Neumann
---
"There's just so many extra children, we could just
feed the
children to these tigers. We don't need them, we're not
doing
anything with them.
Tigers are noble and sleek; children are loud and
messy."
-- Neko Case
Life is unfair. Kill yourself or get over it.
-- Black Box Recorder
Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc.
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http:/
/groups.yahoo.com/group/ocaml_beginners/
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|