add confirm to rm-bg

This commit is contained in:
Q
2025-02-04 12:02:14 +02:00
parent c0c2c46b35
commit e12deaab73

View File

@@ -3,19 +3,42 @@ set -e
function helpexit() { function helpexit() {
echo Delete files in background, by moving the to a temp folder first. echo Delete files in background, by moving the to a temp folder first.
echo This command is always recursive! echo This command is always recursive!
echo ' passing -i will ask first'
exit exit
} }
[[ -z "$1" ]] && helpexit [[ -z "$1" ]] && helpexit
[[ "$1" = "-h" ]] && helpexit [[ "$1" = "-h" ]] && helpexit
CONFIRM=0
for k in "$@"; do
if [[ "$k" = "-i" ]]; then
CONFIRM=1
fi
done
tempfolders=() tempfolders=()
for f in "$@"; do for f in "$@"; do
[[ -e "$f" ]] || { if [[ ! -e "$f" ]]; then
echo "$f" does not exist if [[ "$f" = "-i" ]]; then continue; fi
continue echo "$f" does not exist
} continue
fi
d=$( abs-path $( dirname "$f" ) )/.rm_bg.$$ d=$( abs-path $( dirname "$f" ) )/.rm_bg.$$
if [[ $CONFIRM -eq 1 ]]; then
echo "Delete '$f'?"
read -r -p "Are you sure? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
true
;;
*)
continue
;;
esac
fi
mkdir -p "$d" mkdir -p "$d"
mv "$f" "$d"/ mv "$f" "$d"/
tempfolders+=( "$d" ) tempfolders+=( "$d" )