aboutsummaryrefslogtreecommitdiff
path: root/slides.md
blob: 9a3913f3b819e413633ed8b3d9c64cb6930fe9ca (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
%title: Bash Scripting: It's not so bad
%author: Cody Hiar
%date: 2019-05-27

-> Bash Scripting: It's not so bad <-
=====================================

-------------------------------------------------

# About Me

* Graduated Computer Engineering at U of A
* SaltStack Certified
* Now working remotely @ Blendable
* Vim/Tmux Diehard, also cli in general
* Interests: Python, Automation, DevOps, Linux

# Where I be

* www.codyhiar.com
* www.github.com/thornycrackers

# Past Presentations (available on GitHub)

* Scraping with scrapy (YEGSEC)
* Python Daemons (Edmonton.py)
* Setting Django up on a VPS (Edmonton.py)
* Docker (YEGSEC)

-------------------------------------------------

-> Goals <-
===========

- What's in the Language
- Some best practices
- Resources to help

-------------------------------------------------

-> Why bash scripting? <-
=========================

- Easy win with tools you already have install.
- Scripts are easy to share between different people.
- I don't like repeating myself

^

-> When to bash script: <-
=====================

^

- Everyday. Automate everything. Invaluable for quick throw away scenarios.
  Scripting is a great way to learn how linux/unix systems in general work.

^

-> When not to bash script: <-
=========================

^

- Personal Take: If your script is getting past the 100ish line mark it's
  probably a good time to consider moving to a different language

-------------------------------------------------

-> Bash has it's own language <-
================================

Bash has support for a bunch of the basic programming features you need such as:
Variables, Functions, If/Else, Conditionals, Loops, Arrays, Dictionaries, Case
Switch.

-------------------------------------------------

# Variables

```
NAME="John"
```

# Functions

```
get_name() {
    echo "John"
}
 
echo "You are $(get_name)"
```

# If/Else

```
if [[ -z "$string" ]]; then
    echo "String is empty"
elif [[ -n "$string" ]]; then
    echo "String is not empty"
fi
```

-------------------------------------------------

# Conditionals

```
git commit && git push
git commit || echo "Commit failed"
```

# For Loop

```
for i in {1..5}; do
    echo "Welcome $i"
done
```

# While Loop

```
while true; do
  ยทยทยท
done
```

-------------------------------------------------

-> The "while" poem <-
======================


While true
do foo
sleep 1
done

```
while true; do clear; curl https://duckduckgo.com; sleep 1; done
```

-------------------------------------------------

# Array

```
Fruits=('Apple' 'Banana' 'Orange')
 
echo ${Fruits[0]}  
```

# Dictionaries

```
declare -A sounds
 
sounds[dog]="bark"
 
echo "${sounds[dog]}"
```

# Case Switch

```
case "$1" in
  start | up)
    vagrant up
    ;;
 
  *)
    echo "Usage: $0 {start|stop|ssh}"
    ;;
esac
```

-------------------------------------------------

-> My First Bash Script <-
==========================

1. Run: `touch myscript.sh`
2. Add the following content

```
#!/usr/bin/env bash
echo "Hello World"
```

3. Run: `chmod +x myscript.sh`
4. Run: `./myscript.sh`

-------------------------------------------------

-> What can I put in a shell script <-
======================================

Anything you type into your terminal to run commands can be put into a bash
script. This means that Bash Scripts are essentially a free way to start
automating things that you do on a daily basis without having to install any new
tools.

-------------------------------------------------

-> Example: Postgres Backup <-
================================

```
#!/usr/bin/env bash
 
TODAY=$(date +%F)
FILENAME="database_backup-${TODAY}.sql"
 
if [[ ! -f "$FILENAME" ]]; then 
    pg_dump -h hostname -U myuser > "$FILENAME"
fi
```

-------------------------------------------------

-> Best Practice: Idempotence <-
================================

Plain English: Output or effect is the same if a script is ran multiple times.

^

This isn't limited to Bash Scripting but is a good practice in other DevOps
tools you may be using as well.

-------------------------------------------------

-> Best Practice: Unofficial Strict Mode <-
===========================================

```
#!/usr/bin/env bash
#
# Set options:
#   e: Stop script if command fails
#   u: Stop script if unset variable is referenced
#   x: Debug, print commands as they are executed
#   o pipefail:  If any command in a pipeline fails it all fails
#
# IFS: Internal Field Separator
set -euxo pipefail
IFS=$'\\n\\t'
```
-------------------------------------------------

-> Best Practice: Traps <-
==========================

```
#!/usr/bin/env bash
function finish {
  # Your cleanup code here
}
trap finish EXIT
```

Traps are the equivalent of wrapping your script in a giant `try/catch`
statement so that if anything does fail, the script will clean up after itself.

-------------------------------------------------

-> Best Practice: Linting with Shellcheck <-
============================================


The goals of ShellCheck are

    To point out and clarify typical beginner's syntax issues that cause a
    shell to give cryptic error messages.
     
    To point out and clarify typical intermediate level semantic problems that
    cause a shell to behave strangely and counter-intuitively.
     
    To point out subtle caveats, corner cases and pitfalls that may cause an
    advanced user's otherwise working script to fail under future
    circumstances.

https://github.com/koalaman/shellcheck

-------------------------------------------------

-> Best Practice: Automated testing with bats-core <-
=====================================================

Bats is a TAP-compliant testing framework for Bash. It provides a simple way to
verify that the UNIX programs you write behave as expected.

https://github.com/bats-core/bats-core

-------------------------------------------------

-> Resources <-
===============

- https://explainshell.com/
- https://misc.flogisoft.com/bash/tip_colors_and_formatting
- https://www.bash.academy/
- http://redsymbol.net/articles/bash-exit-traps/
- http://redsymbol.net/articles/unofficial-bash-strict-mode/
- https://devhints.io/bash
- https://github.com/jlevy/the-art-of-command-line
- https://google.github.io/styleguide/shell.xml
- http://www.bashoneliners.com/
- https://www.commandlinefu.com

- https://www.codyhiar.com/snippets/awesome-bookmarks-my-collection/

-------------------------------------------------

-> Questions <-
===============