Version française
Home     About     Download     Resources     Contact us    
Browse thread
commands.getoutput () in ocaml?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: malc <av1474@c...>
Subject: Re: [Caml-list] commands.getoutput () in ocaml?
On Wed, 22 Aug 2007, Luca de Alfaro wrote:

> Dear All,
>
> I am looking for a quick way to do the equivalent of
>
> s = commands.getoutput ("ls " + name + "*")
>
> in Ocaml.
>
> Unix.system does perform a call, but the output of the call is not
> returned... yes, I can redirect it to /tmp/output.ocaml and then read the
> file, or even go crazy and build pipes, but is there a more elegant (read:
> succint) way to do it?

I needed something to that effect yesterday and wrote following:

open Unix;;

let getprocout cmd =
   let ic = open_process_in cmd in
   let b = Buffer.create 10 in
   let rec loop () =
     match try Some (input_line ic) with End_of_file -> None with
       | None -> Buffer.contents b
       | Some line ->
           Buffer.add_string b line;
           loop ()
   in
     loop ()
;;

-- 
vale