Linux course index · Linux Intermediate category · Lesson 4 of 9
AI disclosure — AI-generated content, posted as is. AI can make mistakes. This lesson draws on the linked Open Education posts and adds original explanations or lab examples where useful. The newly written explanations and examples may be freely reused. Linked source material retains its own terms.
Learning goal
Use a case statement, a function, and a loop to generate a multiplication chart and test invalid input.
Concepts and setup
if tests a command’s exit status. case matches a value against shell patterns. for iterates a sequence and while repeats while a condition succeeds. until repeats while its condition fails. Bash functions can use local variables, accept arguments, and return status. Write results to stdout and errors to stderr so another program can use the output.
The site has separate while- and for-loop versions of this multiplication chart. This version checks the argument once and reuses one function rather than copying the same logic into ten case branches.
Build a chart and check the failure path
lab=$(mktemp -d)
cd "$lab" || exit
cat > chart.sh <<'EOF'
#!/usr/bin/env bash
chart() {
local n=$1 i
for ((i=1; i<=10; i++)); do
printf '%d * %d = %d\n' "$n" "$i" "$((n*i))"
done
}
case "$1" in
[1-9]|10) chart "$1" ;;
*) printf 'Usage: %s NUMBER (1-10)\n' "$0" >&2; exit 2 ;;
esac
EOF
bash -n chart.sh
bash chart.sh 9 > chart.txt
head -n 2 chart.txt
tail -n 1 chart.txt
bash chart.sh 11
printf 'status=%s\n' "$?"
Possible output
9 * 1 = 9
9 * 2 = 18
9 * 10 = 90
Usage: chart.sh NUMBER (1-10)
status=2
bash -n checks syntax but does not execute the script. bash -x shows command expansions when diagnosing a lab script. Avoid tracing scripts that handle secrets.
Verify the loop boundary
wc -l < chart.txt
grep -Fx '9 * 10 = 90' chart.txt
bash -x chart.sh 2 > two.txt 2> trace.txt
tail -n 1 two.txt
Possible output
10
9 * 10 = 90
2 * 10 = 20
Checking both the line count and the last value helps catch off-by-one errors.
Questions only
Quick-check questions
- When does the wildcard case branch run?
- What does bash -n detect that it cannot prove about output?
Assessment questions
- Rewrite the for loop as a while loop.
- What should be tested for arguments 1, 10, and 0?
- How would you save compiler-like traces without mixing them with chart output?
Questions and answers
Quick-check answers
- When does the wildcard case branch run?
Answer — When none of the earlier patterns match, including when no argument is given. - What does bash -n detect that it cannot prove about output?
Answer — It detects syntax errors. It cannot prove the chart contains correct values.
Assessment answer key
- Rewrite the for loop as a while loop.
Answer — Set local i=1, then use while ((i<=10)); do, print the row, increment i with ((i++)), and end with done. - What should be tested for arguments 1, 10, and 0?
Answer — 1 and 10 should succeed with 10 rows and correct endpoints. 0 should produce usage on stderr with status 2. - How would you save compiler-like traces without mixing them with chart output?
Answer — Run bash -x chart.sh 2 > two.txt 2> trace.txt in the lab.
Source articles on this site
- While Loop Example in Shell Programming. Display a Chart #Linux/Unix: Shell Programming – 001
- Another Example of Case and For Loop #Linux/Unix: Shell Programming – 001
- Use of For Loops and Functions in Shell Programming #Linux/Unix: Shell Programming – 001
- Topic 2: Example: Debug a Script #Linux/Unix: Shell Programming – 001
- Linux Shell Script: Until Example
- 20 mixed quiz questions on Quoting and Control Statements.
Continue the course
Previous lesson 03 — Bash Variables, Quoting, and Input. Next lesson 05 — Package Managers and Repositories.
Linux course index · Linux Intermediate category · Lesson 4 of 9
