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
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
target="cache"
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"
target ← cache
3target="cache"4if [ -n "$target" ]; thenvalues this stepcachetargetif [ -n "$target" ]; then
3target="cache"4if [ -n "$target" ]; then5 action="check:$target"values this stepcachetargetaction ← check:cache
4if [ -n "$target" ]; then5 action="check:$target"6elsevalues this stepcheck:cacheactioncachetargetecho "$action"
8fi9echo "$action"outputcheck:cachevalues this stepcheck:cacheaction
target ← (empty)
3target=""4if [ -n "$target" ]; thenvalues this step(empty)targetif [ -n "$target" ]; then
3target=""4if [ -n "$target" ]; then5 action="check:$target"values this step(empty)targetaction ← skip:missing-target
6else7 action="skip:missing-target"8fivalues this stepskip:missing-targetactionecho "$action"
8fi9echo "$action"outputskip:missing-targetvalues this stepskip:missing-targetaction
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.