summaryrefslogtreecommitdiffstats
path: root/guides/shell-workshop/session.html
blob: 1a5da6ea668d4616ea711bbb888d7dadd8ae7232 (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<h2>Shell Input</h2>

<pre><code>sh@sleipnir1:~$ vim hello.sh
sh@sleipnir1:~$ cat hello.sh
echo Hello world
sh@sleipnir1:~$ sh hello.sh
Hello world
sh@sleipnir1:~$ sh -c 'echo Hello world'
Hello world
sh@sleipnir1:~$ echo Hello world
Hello world
</code></pre>

<h2>Comments, Magic Number Behavior, and File Modes</h2>

<pre><code>sh@sleipnir1:~$ vim hello.sh
sh@sleipnir1:~$ cat hello.sh
#! /bin/sh
# This is a comment
echo Hello world
sh@sleipnir1:~$ ./hello.sh
-su: ./hello.sh: Permission denied
sh@sleipnir1:~$ ls -l hello.sh
-rw-r--r-- 1 sh sh 48 Mar 30 13:26 hello.sh
sh@sleipnir1:~$ chmod a+x hello.sh
sh@sleipnir1:~$ ls -l hello.sh
-rwxr-xr-x 1 sh sh 48 Mar 30 13:26 hello.sh
sh@sleipnir1:~$ ./hello.sh
Hello world
</code></pre>

<h2>Field Splitting and Quoting</h2>

<pre><code>sh@sleipnir1:~$ mkdir foo bar
sh@sleipnir1:~$ ls
bar  foo  hello.sh
sh@sleipnir1:~$ rmdir foo bar
sh@sleipnir1:~$ mkdir foo\ bar
sh@sleipnir1:~$ ls
foo bar  hello.sh
sh@sleipnir1:~$ rmdir foo\ bar
sh@sleipnir1:~$ ls
hello.sh
sh@sleipnir1:~$ mkdir 'foo bar
&gt; baz'
sh@sleipnir1:~$ ls
foo bar?baz  hello.sh
sh@sleipnir1:~$ rmdir 'foo bar
&gt; baz'
sh@sleipnir1:~$ ls
hello.sh
sh@sleipnir1:~$ mkdir "foo bar baz"
sh@sleipnir1:~$ ls
foo bar baz  hello.sh
</code></pre>

<h2>Tilde Expansion</h2>

<pre><code>sh@sleipnir1:~$ echo ~
/home/sh
sh@sleipnir1:~$ ls ~sh-foo
another-directory  another-file  bar  baz  foo  some-directory  some-file
sh@sleipnir1:~$ ls ~sh-foo/some-directory
Hey, this is a cool file!
</code></pre>

<h2>Pathname Expansion</h2>

<pre><code>sh@sleipnir1:~$ echo ~sh-foo/*-file
/home/sh-foo/another-file /home/sh-foo/some-file
sh@sleipnir1:~$ echo ~sh-foo/ba*/file
/home/sh-foo/bar/file /home/sh-foo/baz/file
sh@sleipnir1:~$ echo ~sh-foo/ba[a-r]/file
/home/sh-foo/bar/file
sh@sleipnir1:~$ echo ~sh-foo/ba[p-z]/file
/home/sh-foo/bar/file /home/sh-foo/baz/file
</code></pre>

<h2>Parameter Assignment</h2>

<pre><code>sh@sleipnir1:~$ myparam=foo
sh@sleipnir1:~$ mynum=1
</code></pre>

<h2>Parameter Expansion</h2>

<pre><code>sh@sleipnir1:~$ echo ${myparam}
foo
sh@sleipnir1:~$ echo ${nullparam:-default}
default
sh@sleipnir1:~$ echo ${#myparam}
3
sh@sleipnir1:~$ file=main.c
sh@sleipnir1:~$ echo ${file%.c}.o
main.o
sh@sleipnir1:~$ dir=foo/bar/baz
sh@sleipnir1:~$ echo ${dir##*/}
baz
</code></pre>

<h2>Positional Parameters</h2>

<pre><code>sh@sleipnir1:~$ vim pos-params.sh
sh@sleipnir1:~$ cat pos-params.sh
#! /bin/sh
echo ${1}
echo ${2}
sh@sleipnir1:~$ chmod a+x pos-params.sh
sh@sleipnir1:~$ ./pos-params.sh foo
foo

sh@sleipnir1:~$ sh pos-params.sh foo bar baz
foo
bar
</code></pre>

<h2>Special Parameters</h2>

<pre><code>sh@sleipnir1:~$ vim spec-params.sh
sh@sleipnir1:~$ cat spec-params.sh
#! /bin/sh

echo Number of positional parameters: ${#}
echo Positional parameters: ${@}
echo Script name: ${0}
sh@sleipnir1:~$ sh spec-params.sh foo bar baz
Number of positional parameters: 3
Positional parameters: foo bar baz
Script name: spec-params.sh
</code></pre>

<h2>Exit Statuses</h2>

<pre><code>sh@sleipnir1:~$ vim spec-params.sh
sh@sleipnir1:~$ cat spec-params.sh
#! /bin/sh

echo Number of positional parameters: ${#}
echo Positional parameters: ${@}
echo Script name: ${0}
true
echo ${?}
false
echo ${?}
sh@sleipnir1:~$ sh spec-params.sh foo bar baz
Number of positional parameters: 3
Positional parameters: foo bar baz
Script name: spec-params.sh
0
1
</code></pre>

<h2>Shell Variables and Links <code>.</code> and <code>..</code></h2>

<pre><code>sh@sleipnir1:~$ echo ${HOME}
/home/sh
sh@sleipnir1:~$ echo ${PWD}
/home/sh
sh@sleipnir1:~$ ls
foo bar baz  hello.sh  pos-params.sh  spec-params.sh
sh@sleipnir1:~$ cd foo\ bar\ baz/
sh@sleipnir1:~/foo bar baz$ echo ${PWD}
/home/sh/foo bar baz
sh@sleipnir1:~/foo bar baz$ ls .
sh@sleipnir1:~/foo bar baz$ mkdir file1 file2
sh@sleipnir1:~/foo bar baz$ ls .
file1  file2
sh@sleipnir1:~/foo bar baz$ ls ..
foo bar baz  hello.sh  pos-params.sh  spec-params.sh
sh@sleipnir1:~/foo bar baz$ cd ..
sh@sleipnir1:~$ echo ${PWD}
/home/sh
sh@sleipnir1:~$ PS1='$ '
$ PS1='PROMPT&gt; '
PROMPT&gt; echo Hello
Hello
PROMPT&gt; PS1='$ '
$ echo Hello \
&gt; World
Hello World
</code></pre>

<h2>Command Substitution</h2>

<pre><code>$ filepath=$(basename ~sh-foo/bar/file)
$ echo ${filepath}
file
</code></pre>

<h2>Arithmetic Expansion</h2>

<pre><code>$ echo $((2+3))
5
$ echo $((0x10 + 4))
20
$ i=0
$ i=$(($i + 1))
$ echo ${i}
1
$ i=$(($i + 1))
$ echo ${i}
2
$ i=$(($i + 1))
$ echo ${i}
3
</code></pre>

<h2>Simple Commands</h2>

<pre><code>$ i=0 echo Hello
Hello
</code></pre>

<h2>Command Search and Execution</h2>

<pre><code>$ echo ${PATH}
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
$ ./hello.sh
Hello world
$ hello.sh
-su: hello.sh: command not found
$ ls
foo bar baz  hello.sh  pos-params.sh  spec-params.sh
$ PATH=${PATH}:${HOME}
$ echo ${PATH}
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/sh
$ hello.sh
Hello world
</code></pre>

<h2>Pipelines</h2>

<pre><code>$ ! true
$ echo ${?}
1
$ ! false
$ echo ${?}
0
$ true
$ echo ${?}
0
$ false
$ echo ${?}
1
</code></pre>

<h2>Lists</h2>

<pre><code>$ false &amp;&amp; echo foo || echo bar
bar
$ true || echo foo &amp;&amp; echo bar
bar
</code></pre>

<h2><code>test</code> Command and <code>if</code> Construct</h2>

<pre><code>$ [ foo = foo ] &amp;&amp; echo equal || echo not equal
equal
$ if [ foo = foo ]; then echo equal else echo not equal; fi
equal else echo not equal
$ if [ foo = foo ]; then echo equal; else echo not equal; fi
equal
$ if [ foo = foo ]; then
&gt;     echo equal
&gt; else
&gt;     echo not equal
&gt; fi
equal
</code></pre>

<h2><code>while</code> Loop</h2>

<pre><code>$ i=0
$ while [ ${i} -lt 5 ]; do
&gt;     echo ${i}
&gt;     i=$(($i + 1))
&gt; done
0
1
2
3
4
$ vim param-loop.sh
$ cat param-loop.sh
while [ ${#} -gt 0 ]; do
    echo Parameter: ${1}
    shift
done
$ sh param-loop.sh foo bar baz
Parameter: foo
Parameter: bar
Parameter: baz
</code></pre>

<h2><code>for</code> Loop</h2>

<pre><code>$ for param in foo bar baz; do
&gt;     echo ${param}
&gt; done
foo
bar
baz
</code></pre>

<h2><code>case</code> Construct</h2>

<pre><code>$ param=foo
$ case ${param} in
&gt;     f*)
&gt;         echo starts with f
&gt;         ;;
&gt;     b*)
&gt;         echo starts with b
&gt;         ;;
&gt; esac
starts with f
</code></pre>

<h2>Functions</h2>

<pre><code>$ print_hello()
&gt; {
&gt;     echo Hello world
&gt; }
$ print_hello
Hello world
$ print_params()
&gt; {
&gt;     echo ${@}
&gt; }
$ print_params 1 2 3
1 2 3
</code></pre>

<h2>Input/Output Redirection</h2>

<pre><code>$ echo Hello world &gt; hello-file
$ cat hello-file
Hello world
$ echo Hello world &gt; hello-file
$ echo Hello world &gt; hello-file
$ echo Hello world &gt; hello-file
$ cat hello-file
Hello world
$ echo Hello world &gt;&gt; hello-file
$ echo Hello world &gt;&gt; hello-file
$ cat hello-file
Hello world
Hello world
Hello world
$ vim read-file.sh
$ cat read-file.sh
while read line; do
    echo Line: ${line}
done &lt; myfile.txt
$ sh read-file.sh
Line: line one
Line: line two
Line: some other line
$ vim read-file.sh
$ cat read-file.sh
while read field1 field2; do
    echo Line: ${field1}, ${field2}
done &lt; myfile.txt
$ sh read-file.sh
Line: line, one
Line: line, two
Line: some, other line
$ echo Hello world &gt; /dev/null
$ sh -c not-a-command
sh: 1: not-a-command: not found
$ sh -c not-a-command 2&gt; /dev/null
$ while read line; do
&gt;     echo Line: ${line}
&gt; done &lt;&lt;EOF
&gt; this is input
&gt; some more input
&gt; EOF
Line: this is input
Line: some more input
</code></pre>