Skip to content
← System engineer foundations

Practical lab guide

Lab: test a read-only diagnostic script

Exercise the script contract with controlled inputs and keep a compact revision record.

Documentation reviewed2026-10-01 · 3 min read · lab time varies
On this page

Use the script from the quoting bite

Create a new Linux practice directory and save inspect-path.sh from the Bash arguments lesson. Keep it read-only and run it without elevated privileges. The script's job is only to inspect one path and report a useful failure.

Create a fixture named file with spaces.txt. Run syntax checking, then the following cases individually, capturing each exit status immediately:

InputExpected contract
Current directorySuccessful inspection
Quoted fixture pathOne argument, successful inspection
No argumentsUsage error, exit 2
Missing pathError, exit 1
Two argumentsUsage error, exit 2

If the file disappears between the existence check and inspection, the final command can fail too. Record this time-of-check limitation instead of treating it as impossible.

Make the input matrix reproducible

Inside the chosen Linux practice directory, confirm the diagnostic exists, then create only these fixtures:

bash
printf 'space fixture\n' > 'file with spaces.txt'
printf 'hyphen fixture\n' > ./-study-file
bash -n inspect-path.sh
bash inspect-path.sh 'file with spaces.txt'
printf 'spaced-path status: %s\n' "$?"
bash inspect-path.sh -study-file
printf 'hyphen-path status: %s\n' "$?"
bash inspect-path.sh . 'file with spaces.txt'
printf 'two-argument status: %s\n' "$?"

Expected statuses are 0, 0, and 2. The hyphen case exercises the diagnostic's ls -- handling. Ensure the “missing path” name actually does not exist before testing it. Use the earlier check-usage.sh test as well if you created it; it checks stream placement in addition to status.

Do not put the whole deliberate-failure matrix under set -e and assume it will finish: some cases must fail for the test to be correct. Capture each result or place it in an explicit conditional assertion.

Add a fixture-based assertion

Save test-inspect.sh beside the diagnostic:

bash
#!/usr/bin/env bash
if bash inspect-path.sh >/dev/null 2>&1; then
  printf 'FAIL: missing argument was accepted\n' >&2
  exit 1
else
  study_status=$?
  if [[ $study_status -ne 2 ]]; then
    printf 'FAIL: expected usage status 2, got %s\n' "$study_status" >&2
    exit 1
  fi
fi
printf 'usage-contract check passed\n'

Run it from that directory. Then deliberately change the diagnostic's usage exit code, observe the test fail, and restore it. This demonstrates that the test checks the chosen contract rather than always printing a success message.

To test a real behavioral difference, temporarily change exit 2 to exit 3 only in the diagnostic's usage branch. The test below should report that mismatch rather than print success. Restore the original and rerun. Then explain which row would catch an accidental unquoted path expansion and which would catch an incorrect argument-count check.

Remove the extra hyphen fixture explicitly with rm -- ./-study-file. Remove any course-owned usage.out/usage.err after inspecting them, and retain or retire the scripts deliberately. -- ensures the filename is data rather than an option.

Cleanup and checkpoint

Run ShellCheck if installed and explain any remaining finding. Remove the named fixture file. Keep the two scripts in a practice repository or delete only those files and the empty practice directory.

Your evidence should include the input matrix, actual statuses, one deliberate test failure, and the repair. Do not count lint or one passing test as proof of every execution environment.

Revision: quote inputs, separate streams, capture status immediately, test failure paths, avoid target mutation. Python automation begins in the next path after these shell fundamentals.

Sources

Primary references: Bash exit status↗; ShellCheck↗.

Your notes and evidence

Record observations, questions, or links to your work. Keep credentials out of your notes.

Loading saved progress…

Back up or restore this path

Progress and notes stay in this browser. A backup contains only this learning path.