Practice & Assessment
Test your understanding of Functions and Script Architecture
Multiple Choice Questions
5What does the `local` keyword do inside a Bash function?
What does `source lib/utils.sh` do differently from `bash lib/utils.sh`?
In `getopts ":vo:e:h"`, what does the leading colon mean?
What is `${BASH_SOURCE[0]}` and why is it used instead of `$0` in libraries?
How should a Bash function return a string value to its caller?
Coding Challenges
1Reusable Bash Library
Create a library file `lib/utils.sh` with these functions: `require_command(cmd)` — exits with error if a command is not installed; `confirm(prompt)` — prompts yes/no and returns 0/1; `retry(max_attempts, delay, cmd...)` — retries a command up to N times with a delay between attempts, logging each attempt. Write a `test_utils.sh` script that sources the library and calls each function to verify it works. All functions must use `local` for every variable. Input: library is sourced; functions are called with specific arguments. Output: meaningful messages per function. Time estimate: 25-30 minutes.
Mini Project
Modular Deployment Script
Build a multi-file deployment system: `deploy.sh` (main entry point), `lib/logging.sh` (colored log functions from lesson 3.4), `lib/checks.sh` (preflight check functions), `lib/deploy.sh` (deployment functions). The main script must: parse `--env`, `--app`, `--version`, and `--dry-run` flags using getopts; source all libraries using `${BASH_SOURCE[0]}`-relative paths; run preflight checks (required commands installed, env valid, version format correct); simulate or execute deployment steps logged at appropriate levels; write a structured deploy log to `./logs/deploy-TIMESTAMP.log`. All functions use `local`. Dry-run mode prints what would happen without executing.
