Add lesson 3: Pipes and redirection

Sam Okafor 1 year ago
parent 6569723b4c commit b29576d46f2647972e7dc6e7ba1b5d1dfee19ced
3 changed files with 30 additions and 0 deletions
+19-0lessons/03-pipes-and-redirection/README.md ADDED
@@ -0,0 +1,19 @@
1+# Lesson 3: Pipes and redirection
2+
3+Pipes and redirection comes up in almost every real shell project. We start with the why, then write code together.
4+
5+Full lesson: [https://glibcode.net/courses/linux-shell/lessons/3](https://glibcode.net/courses/linux-shell/lessons/3)
6+
7+## Files
8+
9+- `words.sh`: a pipeline that counts the most common words
10+
11+## Run it
12+
13+```sh
14+sh words.sh
15+```
16+
17+## Exercises
18+
19+See [exercises.md](exercises.md). Ask in the forum once you've tried.
+5-0lessons/03-pipes-and-redirection/exercises.md ADDED
@@ -0,0 +1,5 @@
1+# Exercises: lesson 3
2+
3+1. Run `words.sh` and change one value. Write down what you expect, then run it again and compare.
4+2. Combine this lesson with the previous one: use something from both in a small program of your own.
5+3. Look up one function or command the example uses in the official documentation. Note one option you didn't know.
+6-0lessons/03-pipes-and-redirection/words.sh ADDED
@@ -0,0 +1,6 @@
1+# The ten most common words in a book
2+tr -cs 'A-Za-z' '\n' < book.txt | tr 'A-Z' 'a-z' \
3+ | sort | uniq -c | sort -rn | head -10
4+
5+# Save errors and output separately
6+ls /nonexistent /tmp > out.txt 2> errors.txt