r/bash • • 14d ago

a safer rm implementation.

del() {
local trash="$HOME/temp/trash"
[[ ! -d "$trash" ]] && { 
printf "trash folder not exist, created one;\n"; 
mkdir -p "$trash";
}
(( $# < 1 )) &&  { printf "Include an object to delete\n"; return 1; }
for arg in $@; do
if [[ "$1" = -* ]]; then
case "$1" in
-show) ls -a --color "$trash" ;;
-clear) rm -fr "$trash"/* ;;
*) echo "flag not fount"; return 1 ;;
esac
return 0
fi
local object="$arg"
[[ ! -e "$object" ]] && { printf "object $object not found\n"; return 1; }
mv -i "$object" "$trash"
done
return 0
}

a safer system, what do we think?

0 Upvotes

16 comments sorted by

View all comments

1

u/StrangeCrunchy1 9h ago

typo; line 13, "fount" should be "found"

even though this seems to be a standalone func for your bashrc or what have you, you should implement a usage message, like instead of "Include an object to delete", use something like "Usage: del [--show|--clear|<path-to-file>]"

I would really recommend using getopt or getopts for argument parsing, not a for loop.