Appendix B: Final Exam
Goal: Show without instructions that you write robust Bash scripts: new tools with error handling, an analysis with awk, a check for monitoring, tests and a timer. · Duration: approx. 90 min · Prerequisite: Day 15
Rules of Play
- Each exercise only describes the goal. You find the way yourself; the days in parentheses help you look things up.
- Each exercise ends with a check block: individual commands with what they must show. Only when all of them match does the exercise count.
- New tools go into
~/sh-lab/werkzeug/bin, loadlib.shand passci.sh– ShellCheck, shfmt and all tests.
source ~/sh-lab/setup/env.sh
mkdir -p ~/sh-lab/anhb && cd ~/sh-lab/werkzeugExercise 1: How many notes?
Write notes-count: it prints the number of notes. If
notes is unreachable, it ends with exit code 1 and a message on stderr.
(Day 4, Day 8)
Check block:
[[ $(notes-count) == $(curl -s "$NOTES_URL/notes" | jq length) ]] && echo "Zahl stimmt"
NOTES_URL=http://127.0.0.1:9 notes-count 2>/dev/null; echo "Exit: $?"Expected:
Zahl stimmt
Exit: 1
Solution
cat > bin/notes-count <<'EOF'
#!/usr/bin/env bash
# notes-count – prints the number of notes
set -euo pipefail
source "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/../lib/lib.sh"
need curl jq
url=$(notes_url)
curl -fsS --max-time 3 "$url/notes" 2>/dev/null | jq length ||
die "nicht erreichbar: $url"
EOF
chmod +x bin/notes-countSave your progress? Sign up for free
Exercise 2: The slowest requests
Write notes-langsam [count] [logfile]: it lists the
slowest requests with duration in milliseconds, method and path, the
slowest first. Default: 5 requests from $NOTES_LOG. (Day 6,
Day 7)
Check block:
notes-langsam 2 tests/fixtures/access.log
notes-langsam | wc -lExpected:
50.0 ms GET /notes
10.0 ms POST /notes
5
Solution
cat > bin/notes-langsam <<'EOF'
#!/usr/bin/env bash
# notes-langsam – the slowest requests: notes-langsam [count] [logfile]
set -euo pipefail
source "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/../lib/lib.sh"
anzahl="${1:-5}"
log="${2:-${NOTES_LOG:?fehlt – source ~/sh-lab/setup/env.sh}}"
[[ $anzahl =~ ^[1-9][0-9]*$ ]] || die "anzahl muss eine Zahl ≥ 1 sein" 2
[[ -r $log ]] || die "nicht lesbar: $log"
awk '{printf "%7.1f ms %s %s\n", $9 * 1000, substr($4, 2), $5}' "$log" |
sort -rn | head -n "$anzahl"
EOF
chmod +x bin/notes-langsamsubstr($4, 2) removes the quote in front of the method.
shfmt wants operators like | and || at the
end of a wrapped line – otherwise ci.sh
reports a difference. sort -rn sorts by the number at the
start of the line – even with leading spaces.
Save your progress? Sign up for free
Exercise 3: Is the backup fresh?
Write notes-backup-check [-d target] hours: exit code 0
and ok: <file> if there is a backup in the backup
folder (default ~/sh-lab/backups) that is newer than the
given number of hours; otherwise a message and exit code 1. Incorrect
usage ends with 2. (Day 5, Day 9, Day 10)
Check block:
notes-backup-check 24 | cut -c1-10
mkdir -p /tmp/alte-backups && touch -d '3 days ago' /tmp/alte-backups/notes-alt.sql.gz
notes-backup-check -d /tmp/alte-backups 24 2>/dev/null; echo "Exit: $?"
notes-backup-check viele 2>/dev/null; echo "Exit: $?"Expected:
ok: notes-
Exit: 1
Exit: 2
Solution
cat > bin/notes-backup-check <<'EOF'
#!/usr/bin/env bash
# notes-backup-check – is there a fresh backup? notes-backup-check [-d target] hours
set -euo pipefail
source "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/../lib/lib.sh"
ziel="$HOME/sh-lab/backups"
if [[ ${1:-} == -d ]]; then
ziel="${2:?-d braucht einen Ordner}"
shift 2
fi
if (($# != 1)) || [[ ! $1 =~ ^[0-9]+$ ]]; then
die "Aufruf: $PROG [-d ziel] stunden" 2
fi
[[ -d $ziel ]] || die "kein Ordner: $ziel"
neu=$(find "$ziel" -maxdepth 1 -name 'notes-*.sql.gz' -mmin "-$(($1 * 60))" \
-printf '%f\n' | sort -r | head -1)
[[ -n $neu ]] || die "kein Backup jünger als $1 h in $ziel"
echo "ok: $neu"
EOF
chmod +x bin/notes-backup-checkSave your progress? Sign up for free
Exercise 4: Tests for the check
Write tests/notes-backup-check.bats with three tests
that work without real backups: fresh backup → 0, only an old backup →
1, text instead of a number → 2. (Day 11)
Check block:
bats --tap tests/notes-backup-check.bats | grep -c '^ok'
./ci.sh | tail -1Expected:
3
ci: alles grün
Solution
cat > tests/notes-backup-check.bats <<'EOF'
#!/usr/bin/env bats
# Tests for bin/notes-backup-check, with a separate folder per test
setup() {
PATH="$BATS_TEST_DIRNAME/../bin:$PATH"
ziel="$BATS_TEST_TMPDIR/backups"
mkdir -p "$ziel"
}
@test "frisches Backup ist ok" {
touch "$ziel/notes-20260101-000000.sql.gz"
run notes-backup-check -d "$ziel" 1
[ "$status" -eq 0 ]
[ "$output" = "ok: notes-20260101-000000.sql.gz" ]
}
@test "nur altes Backup ist ein Fehler" {
touch -d '2 days ago' "$ziel/notes-alt.sql.gz"
run notes-backup-check -d "$ziel" 24
[ "$status" -eq 1 ]
}
@test "Text statt Zahl ist falsche Bedienung" {
run notes-backup-check -d "$ziel" viele
[ "$status" -eq 2 ]
}
EOF$BATS_TEST_TMPDIR is a separate, empty folder for each
test; bats cleans it up afterwards.
Save your progress? Sign up for free
Exercise 5: Check every hour
Set up a systemd timer that runs notes-backup-check 26
every hour. A missing backup should become visible as a failed unit.
Commit all new files of the toolbox. (Day 11, Day 13)
Check block:
systemctl --user is-enabled notes-backup-check.timer
systemctl --user start notes-backup-check.service
systemctl --user show notes-backup-check.service -p Result
git status --short | wc -lExpected:
enabled
Result=success
0
Solution
cat > ~/.config/systemd/user/notes-backup-check.service <<'EOF'
[Unit]
Description=Prüft, ob es ein frisches Backup von notes gibt
[Service]
Type=oneshot
ExecStart=%h/sh-lab/werkzeug/bin/notes-backup-check 26
EOF
cat > ~/.config/systemd/user/notes-backup-check.timer <<'EOF'
[Unit]
Description=Backup-Prüfung jede Stunde
[Timer]
OnCalendar=hourly
Persistent=true
[Install]
WantedBy=timers.target
EOF
systemctl --user daemon-reload
systemctl --user enable --now notes-backup-check.timer 2>/dev/null
git add -A && git commit -qm "Abschlussprüfung: count, langsam, backup-check"26 hours instead of 24: the nightly backup may be a little late without immediately raising an alarm.
Save your progress? Sign up for free
Knowledge test
- Why does every script in this book start with
set -euo pipefail, and which cases does it still not catch? - What is the difference between
"$@"and"$*"? - Why is
for f in $(find …)dangerous, and what is the safe form? - Why is a variable lost that you set in
befehl | while read …; do …; done? - Why do you build JSON with
jq -n --arginstead ofprintf? - How does a script make sure that it never runs twice at the same time, and why is that better than a PID file?
- Why does a script in
sleep 600only react toTERMafter ten minutes, and how do you change that? - Name three differences between a script in your terminal and the same script in cron.
- Why does
ssh host "befehl $x"often need two layers of quoting? - With which single command can you tell whether
notes-checkis a file, a function or an alias?
Answers
-eaborts on errors,-uon unset variables,pipefailmakes errors in pipes visible. Not covered: commands inif,&&/||,$(…)inlocal x=$(…), and functions that run in such conditions (Day 1, 4, 11)."$@"returns each argument individually,"$*"all of them as one text (Day 2).- The output falls apart at spaces and line breaks. Safe:
find … -print0withwhile IFS= read -r -d ''orxargs -0, orfind -exec … {} +(Day 9). - Behind a pipe, the loop runs in a subshell. Remedy:
done < <(befehl)(Day 3, 12). - jq escapes quotes, backslashes and control characters in the text;
with a
",printfbuilds invalid JSON (Day 2, 8). exec 9> lock; flock -n 9– the lock ends automatically with the process, even after a crash. A PID file stays behind (Day 10).- Bash only runs traps once the current command has finished.
sleep 600 & wait $!lets the signal interruptwait(Day 12). - A different
PATH, no variables fromenv.sh,/bin/shinstead of Bash, no terminal, output by mail instead of on the screen (Day 13). - The line is read twice: by your shell and by the shell on the host.
printf '%q'or a script viabash -ssolves this (Day 14). type -a notes-check(Day 0, 15).
What’s next?
- Docker Compose hands-on: the notes stack your scripts have been looking after, from the ground up.
- Container tools hands-on: ShellCheck and hadolint in the image pipeline.
- Kubernetes hands-on: where scripts run as Jobs and CronJobs in the cluster.
- More Bash: The Bash manual (
man bash) is long but complete. For larger tools, Python is worth a look – when a script needs more than a few hundred lines, it is usually time for it.