Skip to content

Instantly share code, notes, and snippets.

@okurka12
Created June 9, 2024 10:25
Show Gist options
  • Save okurka12/289dfbcef6f835cb3526ea527983bfe4 to your computer and use it in GitHub Desktop.
Save okurka12/289dfbcef6f835cb3526ea527983bfe4 to your computer and use it in GitHub Desktop.
utilities for POSIX shell
# ask if something is ok, exit if it's not
# if OPTION_Y variable is "-y", do nothing
prompt_confirm () {
if [ "$OPTION_Y" != "-y" ]; then
printf "%s (y/n) " "$*"
read -r PROCEED
if [ "$PROCEED" != "y" ]; then
echo "abort"
exit
fi
fi
}
# if $1 exists, ask if its ok to delete and then delete
safe_delete () {
if [ -f "$1" ]; then
prompt_confirm "delete $1?"
echo "deleting $1"
rm "$1"
fi
}
# if $1 exists, ask if its ok to overwrite
ask_overwrite () {
if [ -f "$1" ]; then
prompt_confirm "overwrite $1?"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment