Linux Intermediate 04 — Bash Conditions, Loops, Functions, and Debugging

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

  1. When does the wildcard case branch run?
  2. What does bash -n detect that it cannot prove about output?

Assessment questions

  1. Rewrite the for loop as a while loop.
  2. What should be tested for arguments 1, 10, and 0?
  3. How would you save compiler-like traces without mixing them with chart output?

Questions and answers

Quick-check answers

  1. When does the wildcard case branch run?
    Answer — When none of the earlier patterns match, including when no argument is given.
  2. 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

  1. 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.
  2. 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.
  3. 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

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

Leave a Reply