Design a preview-first command
Validate options before allowing an explicit file-writing operation.
Make the default easy to inspect
A reusable administration tool needs a predictable interface. The safest useful default for a change-producing command is often a preview: show the source, destination and intended operation without performing it. A distinct --apply option then expresses intent to make the change.
Parse the complete argument list before writing anything. If an unknown option appears after --apply, the command must still fail without having already modified a file. A simple case-and-shift loop works in Bash 3.2 and avoids differences between platform-specific getopt implementations.
Validate the contract you promise
This exercise requires an existing source, a nonempty destination and different source/destination strings. Paths are supplied within the workspace and are not aliases or links. A production tool handling arbitrary paths would need stronger identity and containment checks; string inequality alone does not prove that two paths refer to different files.
Quote every path when invoking commands. A path containing spaces or Unicode is still one argument, and text that resembles shell syntax must not be evaluated. Repeated value options deliberately use the last supplied value in this small interface. Document choices like that instead of leaving them accidental. Exit status and stderr distinguish invalid requests from a successful preview, while the normal output stays useful to a human reviewing the action.
args=(--source 'source.txt' --output 'report.txt')
printf 'argument=<%s>\n' "${args[@]}"
Put it into practice.
Implement --source PATH (default source.txt), --output PATH (default report.txt), and --apply (default preview). Repeated value options use the last value. Parse all options before writing. Unknown options exit 2 with “Unknown option”; absent values with “Missing option value”; nonexistent source with “Missing source”; empty output or identical source/output strings with “Invalid output”. Supplied paths are workspace paths without aliases or links. On success print exactly mode=preview|apply, source=PATH, output=PATH on separate lines. Preview leaves outputs unchanged; apply copies exact source bytes to output. Preserve spaces and Unicode.
See an explained solution
source_path=source.txt;output_path=report.txt;mode=preview
while [ "$#" -gt 0 ];do
case "$1" in
--apply) mode=apply;shift;;
--source|--output)
[ "$#" -ge 2 ] || { printf 'Missing option value\n' >&2;exit 2; }
if [ "$1" = --source ];then source_path=$2;else output_path=$2;fi
shift 2;;
*) printf 'Unknown option\n' >&2;exit 2;;
esac
done
[ -f "$source_path" ] || { printf 'Missing source\n' >&2;exit 2; }
[ -n "$output_path" ] && [ "$source_path" != "$output_path" ] || { printf 'Invalid output\n' >&2;exit 2; }
if [ "$mode" = apply ];then cp "$source_path" "$output_path" || exit 2;fi
printf 'mode=%s\nsource=%s\noutput=%s\n' "$mode" "$source_path" "$output_path"
The interface separates intent from execution and rejects the complete invalid request before mutation. Consistent defaults, quoted values and explicit statuses make the command predictable in both interactive and scripted use.