Arc Forumnew | comments | leaders | submitlogin
2 points by Pauan 4974 days ago | link | parent

You can use the pipe-from function or the w/pipe-from macro:

  (w/pipe-from x "ls -l"
    (readlines x))
That will collect the output from the command "ls -l" and return it as a list of strings. Since it's a normal output port, you can use any of the standard stuff on it, like readc, readline, etc. I chose to use readlines because I use that the most often when dealing with scripts.

---

If you only want to call a script, and don't care about it's output, you can use system:

  (system "ls -l")
And if you want to collect the output into one giant string, you can combine it with tostring:

  (tostring:system "ls -l")
This returns a single giant string, rather than a list of strings, as in the pipe-from example. Using pipe-from is a bit more verbose, but gives you more power and control, so I prefer it rather than system.


1 point by Pauan 4973 days ago | link

Oh! It seems that w/pipe-from is only in my own personal copy of Arc... it isn't included by default. Here you go:

  (mac w/pipe-from (var expr . body)
    `(let ,var (pipe-from ,expr)
       (after (do ,@body) (close ,var))))
It would appear readlines is also specific to my copy of Arc, so here it is as well:

  (def readlines (x)
    (drain:readline x))
I have included lots of useful stuff like that in my fork of Arc... And here is an obvious shortened version, for the common case of getting the output as a list of strings:

  (def pipe-lines (y)
    (w/pipe-from x y readlines.x))
Now you can just do (pipe-lines "ls -l")

-----

2 points by akkartik 4973 days ago | link

In similar vein I showed a pipe-to a while back: http://arclanguage.org/item?id=14621

-----

1 point by Pauan 4973 days ago | link

I'm trying to get a generic pipe function working... here's the gist of it:

  (w/pipe "grep 'other'"
    (prn "other")
    (readlines))
Should return "other", but instead it's hanging... gotta figure out why. In any case, both pipe-to and pipe-from are now defined in terms of pipe.

---

I've also changed w/pipe-from so it automatically redirects to stdin:

  (w/pipe-from "ls -l" (readlines))

-----

1 point by Pauan 4973 days ago | link

"Should return "other", but instead it's hanging... gotta figure out why."

Ahhh, I got it... it's waiting for the pipe to close:

  (w/pipe "grep 'other'"
    (prn "other")
    (close stdout)
    (readlines))
That makes things trickier...

-----