-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathreference.org
More file actions
205 lines (152 loc) · 4.24 KB
/
Copy pathreference.org
File metadata and controls
205 lines (152 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#+SETUPFILE: ../org-templates/level-1.org
#+TITLE: Pallet Reference - Script
#+DESCRIPTION: Pallet reference documentation for pushing over SSH
#+KEYWORDS: pallet ssh push keys identity
#+property: exports both
Pallet scripts are written using an embedding of shell script in [[http://clojure.org][Clojure]], and
allows for abstraction of operating system variations.
At it's simplest, the =script= macro takes one or more forms and converts them
to shell script.
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(require
'[pallet.stevedore :as stevedore])
(stevedore/script
(ls))
#+END_SRC
* Variables
There is an obvious mismatch between Clojure's immutable data approach, and
shell script's mutable variables.
** Substitution
For accessing the value of a script variable, stevedore uses `deref` or `@`, which
are also used for command substitution.
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(stevedore/script
(deref TMPDIR))
#+END_SRC
#+results:
: ${TMPDIR}
If the variable isn't set, a default can be supplied.
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(stevedore/script
@TMPDIR-/tmp)
#+END_SRC
#+results:
: ${TMPDIR-/tmp}
** Assignment
Values can be defined with `set!`.
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(stevedore/script
(set! x 1))
#+END_SRC
#+results:
: x=1
* Command Substitution
Similar to variable substitution, command substitution uses `deref`.
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(stevedore/script
(set! R @(cat "/etc/redhat-release")))
#+END_SRC
#+results:
: R=$(cat /etc/redhat-release)
* Loops
Shell script `for` loops are mapped to clojure`s `doseq`.
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(stevedore/script
(doseq [x ["a" "b" "c"]]
(println @x)))
#+END_SRC
#+results:
: for x in a b c; do
: echo ${x}
: done
* Functions
Calling a function follows clojure's normal syntax.
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(stevedore/script
(foo x y))
#+END_SRC
#+results:
: foo x y
Defining a function follows a simplified version of clojure's `defn`, and does
not allow for destructuring. Arguments are named and automatically mapped to the
shell function's numbered arguments.
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(stevedore/script
(script (defn foo [x y] (bar x))))
#+END_SRC
#+results:
: script function foo() {
: x=$1
: y=$2
: bar x
: }
* Arrays
Array literals are generated from clojure's `vector` notation.
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(stevedore/script
[1 "2" (quoted 3) :four])
#+END_SRC
#+results:
: (1 2 "3" four)
Array access uses `aget` and `aset`.
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(stevedore/script
(aget foo 1))
#+END_SRC
#+results:
: ${foo[1]}
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(stevedore/script
(aset foo 1 :foo))
#+END_SRC
#+results:
: foo[1]=foo
* Unquoting
One advantage of embedding script in clojure is that we can escape back to
clojure to substitute in values.
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(let [tmp "/tmp"]
(stevedore/script
(ls ~tmp)))
#+END_SRC
#+results:
: ls /tmp
** Expressions
We can also unquote arbitrary clojure expressions.
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(stevedore/script
(ls ~(str "/" "tmp")))
#+END_SRC
#+results:
: ls /tmp
** Splicing
Sometimes we want to add the contents of a collection into a script, without
demarking the boundaries of the collection.
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(let [files ["a" "b" "c"]]
(stevedore/script
(ls ~@files)))
#+END_SRC
#+results:
: ls a b c
* Script functions
Script functions are defined with `pallet.script/defscript` and provide multimethods
for shell script generation. In pallet this is used to abstract away script
differences between operating systems.
#+BEGIN_SRC clojure :tangle src/stevedore.clj
(require
'[pallet.stevedore :as stevedore]
'[pallet.script :as script])
(script/defscript ls [& args])
(stevedore/defimpl ls :default [& args]
(ls ~@args))
(stevedore/defimpl ls [:windows] [& args]
(dir ~@args))
(script/with-template [:windows]
(stevedore/script
(ls a b c)))
#+END_SRC
#+results:
: dir a b c
* Scripting Guide
An excellent reference on shell scripting is available at http://tldp.org/LDP/abs/html/.