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.

target
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"
  1. target ← cache

    3target="cache"4if [ -n "$target" ]; then
    values this stepcachetarget
  2. if [ -n "$target" ]; then

    3target="cache"4if [ -n "$target" ]; then5    action="check:$target"
    values this stepcachetarget
  3. action ← check:cache

    4if [ -n "$target" ]; then5    action="check:$target"6else
    values this stepcheck:cacheactioncachetarget
  4. echo "$action"

    8fi9echo "$action"
    outputcheck:cache
    values this stepcheck:cacheaction
  1. target ← (empty)

    3target=""4if [ -n "$target" ]; then
    values this step(empty)target
  2. if [ -n "$target" ]; then

    3target=""4if [ -n "$target" ]; then5    action="check:$target"
    values this step(empty)target
  3. action ← skip:missing-target

    6else7    action="skip:missing-target"8fi
    values this stepskip:missing-targetaction
  4. echo "$action"

    8fi9echo "$action"
    outputskip:missing-target
    values 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.