(defn sh"Passes the given strings to Runtime.exec() to launch a sub-process.(sh command* options)command can either be inline Strings, or a Seq of Strings.Options are
:in may be given followed by any legal input source forclojure.java.io/copy, e.g. InputStream, Reader, File, byte[],or String, to be fed to the sub-process's stdin.:out may be given followed by :capture, a File, :pass, :err, or a fn. For...
- :capture - the sub-process's stdout will be stored in String or byte array
as specified by :out-enc.
- File - the output is written to the file- :pass - the sub-process's stdout is passed to the main process stdout.
- :err - send stdout to stderr
- fn - The fn is called with the stdout InputStream as an argument (stream isclosed automatically). The fn is run in a Thread, but sh blocks until theThread completes. This can be used, for example, to filter the stream or to
pipe the output to another sh process.
Defaults to :capture:err same options as :out. Defaults to the same value as :out.:in-enc option may be given followed by a String, used as a characterencoding name (for example \"UTF-8\" or \"ISO-8859-1\") toconvert the input string specified by the :in option to thesub-process's stdin. Defaults to UTF-8.If the :in option provides a byte array, then the bytes are passedunencoded, and this option is ignored.:out-enc option may be given followed by :bytes or a String. If aString is given, it will be used as a character encodingname (for example \"UTF-8\" or \"ISO-8859-1\") to convertthe sub-process's stdout to a String which is returned.If :bytes is given, the sub-process's stdout will be storedin a byte array and returned. Defaults to UTF-8.:env override the process env with a map (or the underlying JavaString[] if you are a masochist).:dir override the process dir with a String or java.io.File.
You can bind :env or :dir for multiple operations using with-sh-envand with-sh-dir.sh returns a map of:exit => sub-process's exit code:out => sub-process's stdout (as byte[] or String):err => sub-process's stderr (String via platform default encoding)". . . )
(println (sh ["ls" "-l"] :out :pass))(println (sh "ls" "-l" "/no-such-thing"))
(println (sh "ls" :out :err)
(println (sh "java" "..." :env (merge-env {"CLASSPATH" "/tmp/some.jar"})))
(println (sh "cat" :in (io/file "/tmp/input") :out (io/file "/tmp/out") :err :pass))
(deftest test-sh-piped; This is similar to the sh expr `cat <input> | sort`(let [input (File/createTempFile "shell-unit" nil)](spit input "b\na\n")(with-open [pipe-in (PipedInputStream.)]; Note that we start the second process (sort) first (in a thread), so it is ready; to accept data concurrently. And we block on the generating process, so we; can close the stream it's writing to. We need to close this stream first, or; the 'sort' process will never finish.(let [result (future (sh "sort" :in pipe-in :err :pass))](with-open [pipe-out (PipedOutputStream. pipe-in)](sh "cat" :in input :out pipe-out :err :pass))(is= 0 (:exit @result))(is= "a\nb\n" (:out @result))))))
; Option A - A :pipe option
(sh "echo" :in input :pipe "sort") ; single string command
(sh "echo" :in input :pipe ["wc" "-l"]) ; seq of strings command
(sh "echo" :in input :pipe {:cmd "sort" :env {} :pipe ["wc" "-l"]} ) ; Map command w/ sub-options and nested pipes
; TODO a :pipe-err option
; Option B - A macro, that will redirect the out of one to the in of the next sh form
(sh/pipe
(sh "echo" :in input)
(sh "sort"))
; Option C - nested sh/pipe forms as the :out value
(sh/sh "echo" :in input :out (sh/pipe "sort"))
; which could be threaded, but then needs to be read right-to-left
(->> (sh/pipe "sort")
(sh/sh "echo" :in input :out))
; Option D - nested sh/pipe forms as the :in value
; which reads from the inside out
(sh/sh "sort" :in (sh/pipe "echo" :in input))
; or threaded, which can be read left=to-right
(->> input
(sh/pipe "echo" :in)
(sh/sh "sort" :in))
--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
(->> input
(sh/pipe "echo" :in)
(sh/sh "sort" :in)
(#(s/split % "\n"))
:out
last)
(->> input
(sh/pipe "echo" :in)
(sh/sh "sort" :out (comp last line-seq io/reader) :in))
--
You received this message because you are subscribed to a topic in the Google Groups "Clojure Dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure-dev/A6xFhcPKdws/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to clojure-dev...@googlegroups.com.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure-dev/A6xFhcPKdws/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.
Cees.
--
You received this message because you are subscribed to a topic in the Google Groups "Clojure Dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure-dev/A6xFhcPKdws/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.