Defensive File Operations
Dry Run File Action
Preview or Apply
A dry-run flag lets a script show the intended file action before applying it. The same target can produce a preview or an apply plan.
Program
Play the script to choose dry-run mode and see the file action plan.
dry_run_file_action.sh
#!/usr/bin/env bash
dry_run=
path="build/tmp"
if [ "$dry_run" -eq 1 ]; then
action="would remove $path"
else
action="remove $path"
fi
echo "$action"
#!/usr/bin/env bash
dry_run=
path="build/tmp"
if [ "$dry_run" -eq 1 ]; then
action="would remove $path"
else
action="remove $path"
fi
echo "$action"
dry run
A dry run previews the action without applying it.
apply mode
Apply mode should be explicit and visible.
file action
Destructive file actions are safer when the exact path is displayed first.