Back
sh (clj)
(source)function
(sh & args)
Passes the given strings to Runtime.exec() to launch a sub-process.
Options are
:in may be given followed by any legal input source for
clojure.java.io/copy, e.g. InputStream, Reader, File, byte[],
or String, to be fed to the sub-process's stdin.
:in-enc option may be given followed by a String, used as a character
encoding name (for example "UTF-8" or "ISO-8859-1") to
convert the input string specified by the :in option to the
sub-process's stdin. Defaults to UTF-8.
If the :in option provides a byte array, then the bytes are passed
unencoded, and this option is ignored.
:out-enc 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.
: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.
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)
Examples
clojure
"(ns autodoc.build-html
\"This is the namespace that builds the HTML pages themselves.
It is implemented with a number of custom enlive templates.\"
{:skip-wiki true, :author \"Tom Faulhaber\"}
(:refer-clojure :exclude [empty complement])
(:import [java.util.jar JarFile]
[java.io File FileWriter BufferedWriter StringReader
BufferedInputStream BufferedOutputStream
ByteArrayOutputStream FileReader FileInputStream]
[java.util.regex Pattern])
(:require [clojure.string :as str])
(:use [net.cgrand.enlive-html :exclude (deftemplate)]
[clojure.java.io :only (as-file file writer)]
[clojure.java.shell :only (sh)]
[clojure.pprint :only (pprint cl-format pprint-ident
pprint-logical-block set-pprint-dispatch
get-pretty-writer fresh-line)]
[clojure.data.json :only (pprint-json)]
[autodoc.collect-info :only (contrib-info)]
[autodoc.params :only (params expand-classpath)])
(:use clojure.set clojure.java.io clojure.data clojure.java.browse
clojure.inspector clojure.zip clojure.stacktrace))")
(defn- flush-alerting-writer
[o]
(let [flush-count-atom (atom 0)]
[
(proxy [java.io.BufferedWriter] [o]
(flush []
(proxy-super flush)
(swap! flush-count-atom inc)))
flush-count-atom]))
(deftest test-flush-underlying-prn
[]
(let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
(binding [*out* out
*flush-on-newline* true]
(prn (range 50))
(prn (range 50)))
(is (= @flush-count-atom 2) "println flushes on newline")))
(deftest test-flush-underlying-pprint
[]
(let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
(binding [*out* out
*flush-on-newline* true]
(pprint (range 50))
(pprint (range 50)))
(is (= @flush-count-atom 2) "pprint flushes on newline")))
(deftest test-noflush-underlying-prn
[]
(let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
(binding [*out* out
*flush-on-newline* nil]
(prn (range 50))
(prn (range 50)))
(is (= @flush-count-atom 0) "println flushes on newline")))
(deftest test-noflush-underlying-pprint
[]
(let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
(binding [*out* out
*flush-on-newline* nil]
(pprint (range 50))
(pprint (range 50)))
(is (= @flush-count-atom 0) "pprint flushes on newline")))
babashka/babashka
#!/usr/bin/env bb
(require '[babashka.classpath :refer [add-classpath]])
(require '[clojure.java.shell :refer [sh]])
(require '[clojure.pprint :refer [pprint]])
(def deps '{:deps {cprop {:mvn/version "0.1.17"}}})
(def cp (:out (sh "clojure" "-Spath" "-Sdeps" (str deps))))
(add-classpath cp)
frankiesardo/auto-parcel
(require
'[adzerk.bootlaces :refer :all]
'[clojure.java.shell :as shell]
'[clojure.string :as str])
(def +version+
(let [{:keys [exit out]} (shell/sh "git" "describe" "--tags")
tag (second (re-find #"v(.*)\n" out))]
(if (zero? exit)
(if (.contains tag "-")
(str tag "-SNAPSHOT")
tag)
"0.1.0-SNAPSHOT")))
(deftask clojars []
(comp (build)
(if (.endsWith +version+ "-SNAPSHOT")
(push-snapshot)
(push-release))))
(deftask init []
(with-pre-wrap fileset
(let [dotfiles (System/getenv "DOTFILES")
home (System/getenv "HOME")]
(println (:out (shell/sh "git" "clone" dotfiles (str home "/dotfiles"))))
(println (:out (shell/sh (str home "/dotfiles/init.sh")))))
fileset))
(bootlaces! +version+)
(task-options! push {:ensure-clean false
:tag false})
4clojure/4clojure
(ns foreclojure.git
(:require [clojure.string :as s])
(:use [clojure.java.shell :only [sh]]))
(letfn [(cmd [& args]
(not-empty (s/trim (:out (apply sh args)))))]
;; fetch these at load time rather than on demand, so that it's accurate even
;; if someone checks out a different revision to poke at without restarting
;; the server (eg to diagnose bugs in a release)
(def sha (cmd "git" "rev-parse" "--verify" "HEAD"))
(def tag (cmd "git" "describe" "--abbrev=0" "master")))
zero-one-group/geni
(ns geni.cookbook-02
(:require
[clojure.java.io]
[clojure.java.shell]
[zero-one.geni.core :as g]
[zero-one.geni.ml :as ml]))
(g/show raw-complaints)
(-> complaints
(g/select :complaint-type)
g/show)
(-> complaints
(g/limit 5)
g/show)
(-> complaints
(g/select :complaint-type)
(g/limit 5)
g/show)
(-> complaints
(g/limit 5)
(g/select :complaint-type)
g/show)
(-> complaints
(g/select :complaint-type :borough)
g/show)
(-> complaints
(g/select :complaint-type :borough)
(g/limit 10)
g/show)
(-> complaints
(g/group-by :complaint-type)
g/count
g/show)
(-> complaints
(g/group-by :complaint-type)
g/count
(g/order-by (g/desc :count))
(g/limit 10)
g/show)
(-> complaints
(g/select :complaint-type)
g/value-counts
(g/limit 10)
g/show)
(-> complaints
(g/filter (g/= :complaint-type (g/lit "Noise - Street/Sidewalk")))
(g/select :complaint-type :borough :created-date :descriptor)
(g/limit 3)
g/show)
(-> complaints
(g/filter (g/&&
(g/= :complaint-type (g/lit "Noise - Street/Sidewalk"))
(g/= :borough (g/lit "BROOKLYN"))))
(g/select :complaint-type :borough :created-date :descriptor)
(g/limit 3)
g/show)
(-> complaints
(g/filter (g/= :complaint-type (g/lit "Noise - Street/Sidewalk")))
(g/select :borough)
g/value-counts
g/show)
scicloj/scicloj.ml-tutorials
(ns render-all
(:require [notespace.cli :as cli]
[notespace.api :as note]
[clojure.java.shell]
[nextjournal.clerk :as clerk]))
(println
(clojure.java.shell/sh "mv" "output/index.html" "docs/userguide-models.html"))
(println
(clojure.java.shell/sh "mv" "output/index.html" "docs/polyglot_kmeans.html"))