List Info

Thread: "ocaml_beginners"::[] How to obtain process output on Unix?




"ocaml_beginners"::[] How to obtain process output on Unix?
user name
2006-08-09 15:32:13
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.
   
  Doug

 		
---------------------------------
Stay in the know. Pulse on the new Yahoo.com.  Check it out.


[Non-text portions of this message have been removed]



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/
 


"ocaml_beginners"::[] How to obtain process output on Unix?
user name
2006-08-09 15:56:02
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/
 



"ocaml_beginners"::[] How to obtain process output on Unix?
user name
2006-08-09 15:48:01
On 8/9/06, doug arro <doug_arroyahoo.com> 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.
>
>  Doug

Try Unix.open_process_in, like the following:

let foo = Unix.open_process_in "ls" in
  try
    while true do
      let l = input_line foo in
        Printf.printf "[%s]\n" l
    done
  with _ -> ()

Lars Nilsson


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/
 



[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )