On Mar 19, 2010, at 6:07 AM, TimDaly wrote:
> (defn cmdresult [cmdstr]
> (let [args (into [] (seq (.split cmdstr " ")))]
> (BufferedReader.
> (InputStreamReader.
> (. (. (. Runtime (getRuntime)) (exec args))
> (getInputStream))))))
Why do (into [])? .exec expects a String[], exactly what .split gives you.
I wrote something similar recently (included below, in case you're interested).
You could easily wrap it with a .split to get similar functionality, though
personally I prefer passing the args as a list because that way you can deal
with paths with spaces in them.
-Michael
(defn cmd [command & args]
"Runs command with args and returns its stdout, stderr, and exit status."
(let [process (.start (ProcessBuilder. (into-array (cons command args))))]
(.waitFor process)
(hash-map
:output
(line-seq
(java.io.BufferedReader.
(java.io.InputStreamReader.
(.getInputStream process))))
:error
(line-seq
(java.io.BufferedReader.
(java.io.InputStreamReader.
(.getErrorStream process))))
:status
(.exitValue process))))
(defn do-cmd [command & args]
"Runs command with args and returns its stdout as a seq of lines. Throws
exception on failure."
(let [command (apply cmd command args)]
(if (zero? (:status command))
(:output command)
(throw
(Exception.
(str "Command failed: "
(apply str
(interpose "\n"
(:error command)))))))))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
To unsubscribe from this group, send email to
clojure+unsubscribegooglegroups.com or reply to this email with the words
"REMOVE ME" as the subject.