sribe
System.cmd output in a stream
Is there a fairly direct way to get from System.cmd a stream which when enumerated will return the command output line by line?
:into IO.stream()
Just echos output to stdout as it comes in. It seems that I can get what I need by either creating a process to receive output and using receive there, or dropping down to Erlang and using a port directly. But I’m wondering if there is some higher-level support for this which I am missing?
Most Liked
fuelen
:into parameter accepts Collectable.t(), so you can use an empty list:
> System.shell("echo hello; sleep 1; echo world; sleep 1; echo end;", into: [])
{["hello\n", "world\n", "end\n"], 0}
or create a custom struct and implement Collectable protocol for it.
c4710n
As far as I know, there’s no more higher-level abstractions in Elixir itself.
You have to:
- use
Portdirectly - or, some external packages, like porcelain
Personally, I prefer using Port directly, you can get more examples at:
- elixir - How to capture each line from System.cmd output? - Stack Overflow
- How to start an OS process in Elixir - Stack Overflow
I’m not sure about the context of your requirements. But personally, I always lean towards small and sufficient implementations rather than large and comprehensive ones. From that perspective, a simple
Portwrapper is sufficient.







