summaryrefslogtreecommitdiffstats
path: root/guides/shell-workshop/session.txt
blob: 234c240f5962ef18154f461c150cf56a0ea91eb3 (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
Shell Input
-----------

    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

Comments, Magic Number Behavior, and File Modes
-----------------------------------------------

    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

Field Splitting and Quoting
---------------------------

    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
    > baz'
    sh@sleipnir1:~$ ls
    foo bar?baz  hello.sh
    sh@sleipnir1:~$ rmdir 'foo bar
    > baz'
    sh@sleipnir1:~$ ls
    hello.sh
    sh@sleipnir1:~$ mkdir "foo bar baz"
    sh@sleipnir1:~$ ls
    foo bar baz  hello.sh

Tilde Expansion
---------------

    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!

Pathname Expansion
------------------

    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

Parameter Assignment
--------------------

    sh@sleipnir1:~$ myparam=foo
    sh@sleipnir1:~$ mynum=1

Parameter Expansion
-------------------

    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

Positional Parameters
---------------------

    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

Special Parameters
------------------

    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

Exit Statuses
-------------

    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

Shell Variables and Links `.` and `..`
--------------------------------------

    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> '
    PROMPT> echo Hello
    Hello
    PROMPT> PS1='$ '
    $ echo Hello \
    > World
    Hello World

Command Substitution
--------------------

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

Arithmetic Expansion
--------------------

    $ 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

Simple Commands
---------------

    $ i=0 echo Hello
    Hello

Command Search and Execution
----------------------------

    $ 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

Pipelines
---------

    $ ! true
    $ echo ${?}
    1
    $ ! false
    $ echo ${?}
    0
    $ true
    $ echo ${?}
    0
    $ false
    $ echo ${?}
    1

Lists
-----

    $ false && echo foo || echo bar
    bar
    $ true || echo foo && echo bar
    bar

`test` Command and `if` Construct
---------------------------------

    $ [ foo = foo ] && 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
    >     echo equal
    > else
    >     echo not equal
    > fi
    equal

`while` Loop
------------

    $ i=0
    $ while [ ${i} -lt 5 ]; do
    >     echo ${i}
    >     i=$(($i + 1))
    > 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

`for` Loop
----------

    $ for param in foo bar baz; do
    >     echo ${param}
    > done
    foo
    bar
    baz

`case` Construct
----------------

    $ param=foo
    $ case ${param} in
    >     f*)
    >         echo starts with f
    >         ;;
    >     b*)
    >         echo starts with b
    >         ;;
    > esac
    starts with f

Functions
---------

    $ print_hello()
    > {
    >     echo Hello world
    > }
    $ print_hello
    Hello world
    $ print_params()
    > {
    >     echo ${@}
    > }
    $ print_params 1 2 3
    1 2 3

Input/Output Redirection
------------------------

    $ echo Hello world > hello-file
    $ cat hello-file
    Hello world
    $ echo Hello world > hello-file
    $ echo Hello world > hello-file
    $ echo Hello world > hello-file
    $ cat hello-file
    Hello world
    $ echo Hello world >> hello-file
    $ echo Hello world >> 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 < 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 < myfile.txt
    $ sh read-file.sh
    Line: line, one
    Line: line, two
    Line: some, other line
    $ echo Hello world > /dev/null
    $ sh -c not-a-command
    sh: 1: not-a-command: not found
    $ sh -c not-a-command 2> /dev/null
    $ while read line; do
    >     echo Line: ${line}
    > done <<EOF
    > this is input
    > some more input
    > EOF
    Line: this is input
    Line: some more input