Defensive File Operations
Path Guard
Check a Target
File scripts should check that a target value exists before building a command plan. This keeps missing input from turning into a broad file operation.
Program
Play the script to choose the target and see the guarded action.
path_guard.sh
#!/usr/bin/env bash
target=
if [ -n "$target" ]; then
action="check:$target"
else
action="skip:missing-target"
fi
echo "$action"
#!/usr/bin/env bash
target=
if [ -n "$target" ]; then
action="check:$target"
else
action="skip:missing-target"
fi
echo "$action"
guard
A guard checks that input is safe before a file action is planned.
target
The target names the file or directory the action will use.
empty value
An empty target should skip the operation instead of expanding into a risky path.