babashka/examples/which.clj

18 lines
500 B
Clojure
Raw Permalink Normal View History

2020-04-12 19:13:47 +00:00
#!/usr/bin/env bb
(require '[clojure.java.io :as io])
2020-04-12 19:19:22 +00:00
(defn which [executable]
2020-04-12 19:13:47 +00:00
(let [path (System/getenv "PATH")
paths (.split path (System/getProperty "path.separator"))]
(loop [paths paths]
(when-first [p paths]
(let [f (io/file p executable)]
(if (and (.isFile f)
(.canExecute f))
(.getCanonicalPath f)
(recur (rest paths))))))))
(when-let [executable (first *command-line-args*)]
2020-04-12 19:19:22 +00:00
(println (which executable)))