-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
791 lines (534 loc) · 12.5 KB
/
Copy pathindex.html
File metadata and controls
791 lines (534 loc) · 12.5 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Interactive Shell</title>
<link rel="stylesheet" href="dist/reset.css">
<link rel="stylesheet" href="dist/reveal.css">
<link rel="stylesheet" href="dist/theme/white.css" id="theme">
<!-- Theme used for syntax highlighted code -->
<link rel="stylesheet" href="plugin/highlight/github.min.css" id="highlight-theme"></head>
<body>
<div class="reveal">
<div class="slides">
<section data-markdown ><textarea data-template>
[comment]: # "markdown: { smartypants: true }"
Warning:
Live Editing
Part 1.5 & 2 of 3
---
## Interactive Shell
# vs
## Scripting
---
## Interactive Shell
---
## Scripting
---
**Posix Shell**
(Scripting)
---
The _NOT_-Bash Parts
(Learn to *Un*learn)
---
## Part 1.5 Continued
---
# TREL
---
- **T**ry
- **R**ead (what's on the screen)
- **E**valuate (with your human brain)
- **L**oop (back to try again)
---
The equal and opposite of the REPL.
<small>(the human loop between your fingers, the screen, and your brain)</small>
---
# REPL
---
- **R**ead (from the keyboard)
- **E**valuate (syntax and expressions)
- **P**rint (the error or result)
- **L**oop
---
There is no magic.
<small>(the computer does what you say, not what you <em>want</em>)</small>
---
## 0. Pre-req
Prep work for upcoming exercises.
---
### 0.1 Webi
Make sure [`webi`](https://webinstall.dev/webi) is installed.
```sh
curl -sS https://webi.sh/ | sh
```
---
### 0.2 Friendly, Interactive Shell
1. Get back into [`fish`](https://webinstall.dev/fish)
```sh
fish
```
2. _If_ that fails
```sh
webi fish
```
3. TREL. And try again.
---
### 0.3 Vim Essentials
1. Install [`node`](https://webinstall.dev/node) for [`prettier`](https://webinstall.dev/prettier) (code formatter)
```sh
webi node
node --version
pathman add ~/.local/opt/node/bin
node --version
webi prettier
```
2. Install [`vim-essentials`](https://webinstall.dev/vim-essentials) (formatters & linters)
```sh
webi shfmt shellcheck
webi vim-essentials
```
---
### 0.4 Command Line Utilities
[`aliasman`](https://webinstall.dev/aliasman), [`bat`](https://webinstall.dev/bat), [`curlie`](https://webinstall.dev/curlie), [`jq`](https://webinstall.dev/jq)
```sh
webi aliasman bat curlie jq
```
```md
| `aliasman` | create aliases that work cross shells |
| `bat` | `cat` (view) files with colors & paging |
| `curlie` | wrap `curl` with syntax highlighting |
| `jq` | query JSON files and APIs for specific data |
```
---
### 0.4 Temp Directory
Go to a place we can make junk files.
```sh
pwd
```
```sh
pushd /tmp/
pwd
```
```sh
popd
pwd
```
```sh
pushd /tmp/
```
---
## Interactive Shell
(Continued)
---
### Big Goal
Automate, Test and Debug tasks in Terminal
0. Eratta
1. Auto-complete
2. Text Navigation
3. Variables
4. Pipes
5. & Redirection
---
## 0. Eratta
<kbd>Ctrl ⌃</kbd> + <kbd>s</kbd>
- This is called "scroll lock".
- It freezes _some_ shells (`bash`, `sh`), \
but not all (e.g. `fish`)
<small>(nor any shell spawned from fish)</small>
---
Try in a terminal or console \
where `bash` is the parent shell:
```sh
find /
```
<kbd>Ctrl ⌃</kbd> + <kbd>s</kbd>
<kbd>Ctrl ⌃</kbd> + <kbd>q</kbd>
---
## 1. Autocomplete Goals
Avoid typos by avoiding typing.
> use the tools, Luke
---
### 1.1 Just TREL
⬆️
➡️
🎩 <kbd>tab</kbd> 🪄
Try browsing your history \
(or just typing letters) \
and test the autocomplete.
---
11 out of 10 doctors agree:
> Typing is the leading cause of typos
---
### 2. Navigation Goals
Learn keyboard navigation.
- emacs-style (line)
- vim-style (text)
---
### 2.1 Start & End (of Line)
| | |
| -------------------------------- | ------------------------------------- |
| <kbd>Ctrl ⌃</kbd> + <kbd>a</kbd> | for start (of alphabet), leftmost key |
| <kbd>Ctrl ⌃</kbd> + <kbd>e</kbd> | for 'end' |
| | |
<small>(fyi: these come from emacs, but are nearly-universally supported)</small>
---
### 2.1.1 Exercise
```sh
# type out this line (alias 'setalias' as 'aliasman')
alias setalias='aliasman'
# but instead of typing this out:
# 1. hit Up to see the previous line
# 2. use Ctrl ⌃ + a to move to the start of the line
# 3. add 'set' in front of 'alias'
# 4. use Ctrl ⌃ + e to get back to the end of the line
# 5. hit Enter ⏎ to run it
setalias setalias='aliasman'
```
<small>(note<span>:</span> the output will tell you to run `alias`, but you've already done that)</small>
---
Repeat for muscle memory!
```sh
# make our interactive `cat` a colorful `bat`
alias cat='bat --style=plain --pager=none'
setalias cat='bat --style=plain --pager=none'
```
Feel the burn!
```sh
# give our interactive `curl` colors
alias curl='curlie'
setalias curl='curlie'
```
---
### 2.2 Text Navigation
🔝🇬
⬅️🇭⬇️🇯⬆️🇰➡️🇱
⏭️🇳
---
| <kbd>Shift ⇧</kbd> | 🔄 | does the opposite or extreme |
| ------------------ | --- | ------------------------------- |
| <br> | | |
| `gg`, `G` | 🔝 | **g**ood **g**ame # 1, opposite |
| `hjkl` | 📈 | awesome goes up to the right |
| `n`, `N` | ⏭️ | **n**ext result, opposite |
---
### 2.2.1 Exercise
Let's pick a file with a few dozen lines...
```sh
vim ~/.vimrc
```
(updated by [`vim-essentials`](https://webinstall.dev/vim-essentials))
---
🎗️Reminder
| <kbd>esc</kbd>, `:q!` | quit (immediately) |
| --------------------- | ------------------ |
| `/` | search |
| <kbd>esc</kbd>, `:w` | write (save) |
---
1. Go to the top and bottom
```sh
gg # top of file
G # (the opposite)
```
2. Go all the way... 📈🇭🇯🇰🇱
```sh
h # left
j # down
k # up
l # right
```
3. Search `/` for "the"
```sh
n # next result
N # (the opposite)
```
---
Repeat - build your muscle memory.
Also, try [Vim Adventures](https://vim-adventures.com/).
---
ℹ️ vim-style text navigation works in _many_ places
as we'll see shortly 😉
---
## 3. Variables Goals
Learn to diagnose unexpected results
(quotes, variables, and interpolation)
---
1. What do you think this will output?
```sh
echo It's $100 bills y'all
```
2. Try it.
3. Is anything different what what you thought?
---
1. What's different about this?
```sh
echo "It's $100 bills y'all"
```
2. What will it do differently?
3. Try it.
4. Is anything different what what you thought?
---
What about each of these?
(keep practicing <kbd>Ctrl ⌃</kbd> + <kbd>a</kbd> and <kbd>Ctrl ⌃</kbd> + <kbd>e</kbd>)
```sh
echo It\'s $100 bills y\'all
echo 'It\'s $100 bills y\'all'
echo "It's \$100 bills y'all"
echo \"It's \$100 bills y'all\"
```
---
The file I wanted you to download:
```sh
https://raw.githubusercontent.com/BeyondCodeBootcamp/shell-cheatsheet/main/README.md
```
Too long to fit on a slide!
(time to learn about variables!)
---
Download the url to NOTES.md \
(in the tmp folder)
```sh
# 1. Declare a variable (interactive shell syntax)
# (note the single quotes)
export my_url='https://raw.githubusercontent.com'
# 2. Add more to that variable
# (note the double quotes)
export my_url="$my_url/BeyondCodeBootcamp/shell-cheatsheet"
export my_url="$my_url/main/README.md"
# 3. Download using the variable (interactive shell syntax)
# (note the double quotes, again)
curl -o ./NOTES.md \
"$my_url"
```
---
```sh
/bin/cat ./NOTES.md
cat ./NOTES.md
less ./NOTES.md
bat ./NOTES.md
rm ./NOTES.md
cat ./NOTES.md
```
---
```sh
curl "$my_url"
curl "$my_url" |
bat
curl "$my_url" |
less
curl "$my_url" \
> ./NOTES.md
bat ./NOTES.md
```
---
```sh
curl "$my_url" \
> ./NOTES.md \
2> ./log.txt
bat ./log.txt
```
---
```sh
export my_url='https://api.github.com/search/issues'
export my_url="$my_url?q=type:pr+state:closed"
export my_url="$my_url+author:coolaj86"
export my_url="$my_url&per_page=2&page=1"
```
```sh
curl "$my_url"
curl "$my_url" > \
./gh-issues.json
```
---
```sh
bat ./gh-issues.json
bat < ./gh-issues.json
bat ./gh-issues.json |
jq '.'
cat ./gh-issues.json |
jq '.total_count'
jq '.total_count' < ./gh-issues.json
```
---
```sh
jq '.items' < ./gh-issues.json
jq '.items[]' < ./gh-issues.json
jq '.items[0]' < ./gh-issues.json
jq '.items[].url' < ./gh-issues.json
jq '.items[0].url' < ./gh-issues.json
```
---
| Director | Description |
| :----------- | :----------------------------- | ------------------- |
| `>` | **replace** file with _stdout_ |
| `<` | _stdin_ from file |
| ` | ` | _stdout_ to _stdin_ |
| `2>` | _stderr_ to file |
| upcoming | |
| `>>` | append _stdout_ to file |
| `> /dev/tty` | write to **t**erminal |
---
## vim 1.3
---
```sh
vim my-issues.sh
```
---
`vim` Vowels
| <kbd>Shift ⇧</kbd> | opposite or extreme |
| -----------------: | :----------------------------------- |
| `o` | **o**pen a new line below |
| ⇧ + `O` | _opposite_ of **o**pen below |
| `a` | **a**ppend to right edge of cursor |
| ⇧ + `A` | _extreme_ of **a**pend to right edge |
| `i` | **i**nsert at left edge of cursor |
| ⇧ + `I` | _extreme_ of **i**nsert at left edge |
---
`vim` Deutschlandics
| (wolks | vagon) |
| ---------: | :---------------------------- |
| `u` | **u**ndo |
| `v` | **v**isually select |
| `d` | **d**elete (for Deutschland!) |
| `v` + `du` | ... copy |
| `p` | **p**aste |
---
```vim
:message
```
---
## Scripting 1.1
---
- `PATH`
- `x` bit
- `shebang`
---
```sh
mkdir -p ~/bin
touch ~/bin/my-issues.sh
chmod a+x ~/bin/my-issues.sh
pathman add ~/bin/
my-issues.sh
```
---
```sh
export my_user=coolaj86
export my_limit=100
export my_page=2
my-issues.sh "$my_user" "$my_limit" "$my_page"
my-issues.sh "$my_user"
my-issues.sh "$my_user" "$my_limit"
```
---
```sh
export my_user=coolaj86
export my_limit=100
export my_page=2
my-issues.sh "$my_user" "$my_limit" "$my_page"
my-issues.sh "$my_user" "$my_limit" # default page 1
my-issues.sh "$my_user" # default 10 items
```
---
```sh
echo #!/bin/sh >> ~/bin/my-issues.sh
echo '#!/bin/sh' >> ~/bin/my-issues.sh
my-issues.sh
```
---
## Scripting 1.2
---
- _strict_ mode
- `set -e`
- `set -u`
- _arguments_
- `${0:-} ... ${999:-}`
- `${@:-}`
- `${*:-}`
- _functions_
- `foo() {( ... )}`
- _test_ & `grep`
---
```sh
#!/bin/sh
set -e
set -u
# 1. handle arguments & set vars
my_name="${1:-}"
# 2. define functions
main() { (
echo "${my_name}"
); }
# 3. do stuff
main "foobar"
```
---
```sh
...
echo "${my_name}"
false
echo "I'm bad"
...
```
---
```sh
...
if false; then
echo "I'm good"
else
echo "I'm bad"
fi
...
```
---
```sh
...
my_num=$(( my_num + 1 ))
...
```
---
## Scripting 1.3
---
- _subshells_
- `"$( ... )"`
- _math shells_
- `$(( ... ))`
- _text expansion_
- `{ ... }`
---
```sh
curl https://example.com/
```
```sh
curl 'https://api.github.com/search/issues?q=type:pr+state:closed+author:coolaj86&per_page=100&page=1'
```
---
- function
- return
- test
- exit
---
</textarea></section>
</div>
</div>
<script src="dist/reveal.js"></script>
<script src="plugin/notes/notes.js"></script>
<script src="plugin/markdown/markdown.js"></script>
<script src="plugin/highlight/highlight.js"></script>
<script src="plugin/math/math.js"></script>
<script>
// More info about initialization & config:
// - https://revealjs.com/initialization/
// - https://revealjs.com/config/
Reveal.initialize({
controls : false,
markdown : {smartypants: true},
controls : false,
keyboard : true,
hash : false,
respondToHashChanges : false,
hash: true,
// Learn about plugins: https://revealjs.com/plugins/
plugins: [ RevealMarkdown, RevealHighlight, RevealNotes, RevealMath ]
});
</script>
</body>
</html>