Functions and Modular Scripts

Define inputs, separate data from status, and source only trusted code.

A definition is not a call

report_start() { printf 'report started\n'; }
printf 'before call\n'
report_start
report_start

Expected: before call, then report started twice.

Call a function by name and arguments, without parentheses. Its body runs when called.

Pass boundaries through a wrapper

print_items() {
  local item
  printf 'count=%s\n' "$#"
  for item in "$@"; do printf '<%s>\n' "$item"; done
}
forward_items() { print_items "$@"; }
forward_items 'east host' '*.log' ''

Expected count: 3. Expected items: <east host>, <*.log>, <>.

"$*" would join the arguments into one word.

Local scope is dynamic in Bash

label=global
helper() { label=changed_by_helper; }
outer() {
  local label=outer_local
  helper
  printf 'outer=%s\n' "$label"
}
outer
printf 'global=%s\n' "$label"

Expected: outer=changed_by_helper, then global=global.

A called helper can change its caller's local binding. Give helper temporaries their own local declarations.

Data is not a return status

large_data() { printf '12345\n'; return 0; }
value=$(large_data)
status=$?
printf 'data=%s; status=%s\n' "$value" "$status"

Expected: data=12345; status=0.

Use stdout for data and stderr for diagnostics. return 12345 wraps to status 57 rather than returning that exact data value.

Preserve failure before reporting it

if value=$(require_label); then
  printf 'label=%s\n' "$value"
else
  status=$?
  printf 'original status=%s\n' "$status"
fi

Use the lesson's complete function definition. Wrong count returns 2; empty label returns 1; a valid label returns data with status 0.

if ! require_label; then status=$? captures the negated test's status instead.

A library is executable code

Sourcing runs a file in the current shell. A filename suffix or read permission does not establish trust.

The lesson's inspected library defines audit_readable with this contract:

Input Status
One readable regular file 0
One unsuitable path 1
Wrong argument count 2

Loading the library does not prompt, deploy, collect a host report, or write a log.

Resolve and load deliberately

The caller resolves its script directory and checks both steps:

script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) || exit 1
source "$script_dir/audit_lib.sh" || {
  printf 'library load failed\n' >&2
  exit 1
}

This supports launching from another directory. It does not resolve every symlink deployment layout or prove file ownership is safe.

Check the contract on your VM

Use materials/09/audit_lib.sh and check_input.sh.

Test a spaced path, an empty regular file, a missing path, a directory, and wrong argument counts. Separate stdout, stderr, and status. Rename the library and confirm the caller refuses to proceed.

Then complete the graded lab. Chapter 13 develops systematic debugging and regression tests.

Source context: Linuxize Bash functions, linked in the lessons. Examples and exercises are original adaptations.