04 / RELIABLE ADMINISTRATION TOOLS · LESSON 13

Build preview and apply modes

Validate options and make a controlled configuration change only when apply is requested.

16 min · learn at your pace

Make the safe path the default

An administration tool can show its intended change before writing anything. Preview should be the default, and applying the change should require an explicit option. Both modes should use the same validated value so the preview accurately describes what apply will do.

Bash getopts handles short options without evaluating command text. A leading colon in the option specification lets the script produce its own concise errors for unknown options or missing values. Shift away parsed options and reject unexpected positional arguments rather than silently ignoring a typo.

Validate before creating a temporary file

This exercise accepts retention days from 7 through 90 as canonical decimal text, with no leading zero. Bound the spelling before using shell arithmetic so an enormous value cannot overflow and be mistaken for an allowed number. The existing configuration is trusted, single-line teaching data.

For apply mode, stage the replacement in the same directory and rename it over the target only after writing succeeds. An EXIT trap removes an unused temporary file. This provides an individual-file replacement boundary; it is not a transaction across an entire machine. Only the virtual retention configuration changes, and repeated applications of the same value should produce the same bytes.

Try an example.sh
printf 'Current setting: '
cat config/retention.conf
printf 'Preview is the default; -a will request a virtual-file change.\n'
YOUR TURN

Put it into practice.

Implement bash main.sh [-a] [-r days]. Default mode is preview and default days is 30; -a selects apply. Accept only canonical integers 7–90 without leading zeros. Unknown options, missing values or positional arguments must fail nonzero with Usage: on stderr, no stdout, and no configuration change. config/retention.conf is a trusted existing single line retention_days=N. On success print mode=<preview|apply>, current=<N> and requested=<days>, each on a separate line. Preview must leave it unchanged. Apply replaces it with retention_days=<days> and a newline, using a staged file in config. Repeated apply is idempotent.

See an explained solution
One way to solve it.sh
mode=preview;days=30
usage() { printf 'Usage: main.sh [-a] [-r days]\n' >&2;exit 2; }
while getopts ':ar:' option; do
 case "$option" in a) mode=apply;; r) days=$OPTARG;; *) usage;; esac
done
shift $((OPTIND-1));[ "$#" -eq 0 ] || usage
case "$days" in [1-9]|[1-9][0-9]) ;; *) usage;; esac
[ "$days" -ge 7 ] && [ "$days" -le 90 ] || usage
current=$(sed 's/^retention_days=//' config/retention.conf) || exit 1
if [ "$mode" = apply ]; then
 tmp=$(mktemp './config/retention.XXXXXX') || exit 1
 trap 'rm -f "$tmp"' EXIT
 printf 'retention_days=%s\n' "$days" > "$tmp" || exit 1
 mv "$tmp" config/retention.conf || exit 1
fi
printf 'mode=%s\ncurrent=%s\nrequested=%s\n' "$mode" "$current" "$days"

The same validated argument feeds the preview and apply paths. Invalid input cannot reach the write branch, and a same-directory staged replacement avoids exposing an ordinary partially written configuration file.