• (sh & args)
Passes the given strings to Runtime.exec() to launch a sub-process.

Options are

:in may be given followed by a String specifying text to be fed to the
sub-process's stdin.
:out option may be given followed by :bytes or a String. If a String
is given, it will be used as a character encoding name (for
example "UTF-8" or "ISO-8859-1") to convert the
sub-process's stdout to a String which is returned.
If :bytes is given, the sub-process's stdout will be stored in
a byte array and returned. Defaults to UTF-8.
:return-map
when followed by boolean true, 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 (as byte[] or String)
when not given or followed by false, sh returns a single
array or String of the sub-process's stdout followed by its
stderr
:env override the process env with a map (or the underlying Java
String[] 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-env
and with-sh-dir.

1 Example top

  • ;solve 360 is a CRM google app, RESTful. using curl is an easy way to communicate with RESTful apps.
    
    (ns solve360
      (:use clojure.contrib.shell-out))
    
    (def get-contacts-json-format (sh
      "curl"
      "-u" "360test@testcompany.com:W0I1E2dF2Ce61o6t1g2"
      "-X" "GET" "-H" "Accept: application/json"
      "https://secure.solve360.com/contacts"))
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/shell_out.clj:86 top

(defn sh
  "Passes the given strings to Runtime.exec() to launch a sub-process.

  Options are

  :in    may be given followed by a String specifying text to be fed to the 
         sub-process's stdin.  
  :out   option may be given followed by :bytes or a String. If a String 
         is given, it will be used as a character encoding name (for 
         example \"UTF-8\" or \"ISO-8859-1\") to convert the 
         sub-process's stdout to a String which is returned.
         If :bytes is given, the sub-process's stdout will be stored in 
         a byte array and returned.  Defaults to UTF-8.
  :return-map
         when followed by boolean true, 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 (as byte[] or String)
         when not given or followed by false, sh returns a single
         array or String of the sub-process's stdout followed by its
         stderr
  :env   override the process env with a map (or the underlying Java
         String[] 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-env
  and with-sh-dir."
  [& args]
  (let [opts (parse-args args)
        proc (.exec (Runtime/getRuntime) 
		    (into-array (:cmd opts)) 
		    (as-env-string (:env opts))
		    (as-file (:dir opts)))]
    (if (:in opts)
      (with-open [osw (OutputStreamWriter. (.getOutputStream proc))]
        (.write osw (:in opts)))
      (.close (.getOutputStream proc)))
    (with-open [stdout (.getInputStream proc)
                stderr (.getErrorStream proc)]
      (let [[[out err] combine-fn]
                (if (= (:out opts) :bytes)
                  [(for [strm [stdout stderr]]
                    (into-array Byte/TYPE (map byte (stream-seq strm))))
                  #(aconcat Byte/TYPE %1 %2)]
                  [(for [strm [stdout stderr]]
                    (apply str (map char (stream-seq 
                                            (InputStreamReader. strm (:out opts))))))
                  str])
              exit-code (.waitFor proc)]
        (if (:return-map opts)
          {:exit exit-code :out out :err err}
          (combine-fn out err))))))
Vars in clojure.contrib.shell-out/sh: defn let with-open
Used in 0 other vars

Comments top

No comments for sh. Log in to add a comment.