Apache Commons Exec wrapper for Clojure
(require '[clj-commons-exec :as exec])
(exec/sh ["echo" "hello"]) ; A promise is returned immediately.
;=> #<core$promise$reify__5727@12fa6824: :pending>
@(exec/sh ["echo" "hello"]) ; To get a result, deref it.
;=> {:exit 0, :out "hello\n", :err nil}
(exec/sh ["ls" "-l"] {:dir "/"}) ; Second argument is option map.
options
If you want to have multiple processes piped to each other, you can use sh-pipe. Syntax is like sh :
(exec/sh-pipe ["cat"] ["wc"] {:in "hello world"})
;=> (#<Promise@11ba3c1f: {:exit 0, :out nil, :err nil}>
; #<Promise@59c12050: {:exit 0, :out " 0 2 11\n", :err nil}>)
When piping commands using sh-pipe, the first command that does not exit successfully will additionally have the :exception key (and corresponding Exception) delivered in its result map:
(exec/sh-pipe ["cat" "abc.txt"] ["wc"])
;=> (#<Promise@563da1dc:
; {:exit 1, :out nil,
; :err "cat: abc.txt: No such file or directory\n",
; :exception #<ExecuteException org.apache.commons.exec.ExecuteException:
; Process exited with an error: 1 (Exit value: 1)>}>
; #<Promise@7bff88c3:
; {:exit 0, :out " 0 0 0\n", :err nil}>)
Note: Commands that are waiting for piped input downstream of any such error might receive an empty stream. Most likely, they will finish execution, but their output will be logically incorrect.
Leiningen [org.clojars.hozumi/clj-commons-exec "1.2.0"]
Copyright (C) 2011 FIXME
Distributed under the Eclipse Public License, the same as Clojure.
Can you improve this documentation? These fine people already did:
Takahiro Hozumi, Elango Cheran & Thomas SchankEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close