how do i read data from a unix command?

Luke Gorrie luke@REDACTED
Wed Nov 14 20:53:42 CET 2001


Garry Hodgson <garry@REDACTED> writes:

> is there a way i can read the output of a unix command using something
> akin to the file:read() functions?  i'd like to open a pipe and get back
> a file
> that i can do reads from.  something like:
> 
>     { ok, FP } = file:open( File, read )
> 	file:read( FP, ?BUFSIZE )
> 
> i know about os:cmd(), but that reads the whole thing at once, 
> and the files i'm reading are way too big for that.

Not exactly what you want, but you can use the spawn driver (see
'erlang' module docs for details). e.g.:

  7> open_port({spawn, "echo Hello, world"}, [eof]).
  #Port<0.10>
  8> flush().                                       
  Shell got {#Port<0.10>,{data,"Hello, world\n"}}
  Shell got {#Port<0.10>,eof}
  ok
  9> 

It will send you messages incrementally as output is generated, but as
far as I know you can only receive them async and there is no flow
control available. But this could be added to sys.c :-)

Cheers,
Luke




More information about the erlang-questions mailing list