Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Ocaml equivalent of python's
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] Ocaml equivalent of python's
> I want to learn how to use Unix commands from within an Ocaml program
> but the Ocaml manual confused me more than it helped me to understand
> the Ocaml way.
> 
> In Python I can do the following;
> ------------------------------
> import commands, os
> 
> listing = os.system ('ls -laprt') #or any other unix command
> 
> print listing # to get the output of the unix command.
> 
> address = 146.232.128.13
> result = commands.getstatusoutput('ping -c 1 %s' % address,)
> 
> print result
> -----------------------------------
> What would the equivalent be in Ocaml?

I don't know Python, so I can only guess at what you're trying to do.
But here is a sketch:

  let ping_address addr = Sys.command("ping -c 1 " ^ address)

This function runs "ping" and returns it return code, as an integer.
For your first example:

  let ic = Unix.open_process_in("ls -laprt") in
  (* read from the input channel ic with input_char, input_line,
     input, etc.  When finished: *)
  Unix.close_process_in ic

Hope this helps,

- Xavier Leroy
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr