Shell Scripting and Network Automation
This domain tests reliable shell scripts that configure, query, and monitor networked Unix systems. CUNP questions focus on quoting, exit status, safe remote execution, idempotency, scheduling, and avoiding scripts that work only in an interactive shell.
Core rules and concepts
- Quote variable expansions unless word splitting is intended: use "$var" and "$(command)". Unquoted values can expand into multiple arguments or glob patterns.
- Check exit status. set -e stops on many failures but has exceptions; set -u catches unset variables; set -o pipefail exposes failures in pipelines. Combine as set -euo pipefail when appropriate.
- Use trap for cleanup and lock release. Use flock to prevent concurrent runs that could corrupt state.
- Parse options with getopts or a case loop. Avoid parsing ls output; use find -print0 with while IFS= read -r -d ''.
- For SSH automation, use BatchMode=yes to fail instead of prompting, specify IdentityFile, and manage known_hosts deliberately. Do not silently disable host-key checking in production.
- Cron has a minimal environment and a different PATH. systemd timers provide logging and dependency control. Log script actions with timestamps and nonzero exits.
- Idempotency means rerunning produces the same state, not duplicate addresses, rules, or users.
Worked example
A backup script must copy files to a remote host and fail visibly. Use ssh -o BatchMode=yes -o ConnectTimeout=10 backup@host true as a preflight, then rsync over SSH with --archive and explicit paths. Wrap the run in flock -n so overlapping cron jobs exit. Capture the exit status, log it, and exit nonzero on failure. If the remote host key is unknown, fail and require an administrator to add the verified key rather than accepting any key automatically.
Traps that cost marks
Do not leave variables unquoted, ignore command failures, or assume cron has your interactive PATH. Do not use StrictHostKeyChecking=no as a default automation habit. Avoid non-idempotent commands that add a route or rule on every run. Test scripts with set -u and unusual filenames.
Exam focus
Write scripts that fail safely, log evidence, and can be rerun without side effects.