Skip to content

Commit fdde14e

Browse files
committed
Add Specify the Directory of a Shell Command as a clojure til.
1 parent b180339 commit fdde14e

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
99

1010
### clojure
1111

12+
- [Specify the Directory of a Shell Command](clojure/specify-the-directory-of-a-shell-command.md)
1213
- [Splitting On Whitespace](clojure/splitting-on-whitespace.md)
1314
- [Type of Anything](clojure/type-of-anything.md)
1415

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Specify the Directory of a Shell Command
2+
3+
Clojure gives us access to Java's shell capabilities through
4+
`clojure.java.shell`. For instance, if you want to list the contents of your
5+
project's directory, you can issue an `ls` command:
6+
7+
```
8+
> (clojure.java.shell/sh "ls")
9+
; {:exit 0,
10+
; :out "LICENSE\nREADME.md\ndoc\nproject.clj\nresources\nsrc\ntarget\ntest\n",
11+
; :err ""}
12+
```
13+
14+
The default will always be to execute the command in the directory of the
15+
containing project. It is likely that you'd like to specify a different
16+
directory though. There is a function for that:
17+
18+
```clojure
19+
(clojure.java.shell/with-sh-dir "some/dir" (clojure.java.shell/sh "ls"))
20+
```
21+
22+
Or more concisely, you can specify the directory as part of the `sh`
23+
function:
24+
25+
```clojure
26+
(clojure.java.shell/sh "ls" :dir "some/dir")
27+
```

0 commit comments

Comments
 (0)