1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Specify the Directory of a Shell Command as a clojure til.

This commit is contained in:
jbranchaud
2015-04-05 12:20:31 -05:00
parent b1803394b1
commit fdde14efdb
2 changed files with 28 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
### clojure
- [Specify the Directory of a Shell Command](clojure/specify-the-directory-of-a-shell-command.md)
- [Splitting On Whitespace](clojure/splitting-on-whitespace.md)
- [Type of Anything](clojure/type-of-anything.md)

View File

@@ -0,0 +1,27 @@
# Specify the Directory of a Shell Command
Clojure gives us access to Java's shell capabilities through
`clojure.java.shell`. For instance, if you want to list the contents of your
project's directory, you can issue an `ls` command:
```
> (clojure.java.shell/sh "ls")
; {:exit 0,
; :out "LICENSE\nREADME.md\ndoc\nproject.clj\nresources\nsrc\ntarget\ntest\n",
; :err ""}
```
The default will always be to execute the command in the directory of the
containing project. It is likely that you'd like to specify a different
directory though. There is a function for that:
```clojure
(clojure.java.shell/with-sh-dir "some/dir" (clojure.java.shell/sh "ls"))
```
Or more concisely, you can specify the directory as part of the `sh`
function:
```clojure
(clojure.java.shell/sh "ls" :dir "some/dir")
```