我已经制作了这个脚本,但是我很难为这种情况编写代码:在垃圾桶里不应该有同名的文件;在这种情况下,它们应该被重命名。
我该如何解决?
这是我的代码:
#!/bin/bash
help() {
echo "Options:"
echo "\"safe-rm pathname\" to delete, where the pathname can be absolute or relative"
echo "\"safe-rm --recover original pathname\" (including the /) to recover and restore a file or a directory in the original position"
echo "\"safe-rm --list\" to lists the trashcan's content"
echo "\"safe-rm --search\" to search a file in the trashcan"
echo "\"safe-rm --delete-older-than\" to delete files older than certain days"
}
delete() {
if [ ${PARAM1:0:1} = "/" ]; then
echo "You have insert an absolute pathname"
mkdir -p $(dirname $TRASH$PARAM1)
mv $PARAM1 $TRASH$PARAM1
else
echo "You have insert a relative pathname"
mkdir -p $(dirname $TRASH$(pwd)/$PARAM1)
mv $PARAM1 $TRASH$(pwd)/$PARAM1
fi
}
readonly TRASH=$HOME/.Trash;
readonly PARAM1=$1;
readonly PARAM2=$2;
mkdir -p $TRASH;
case "$PARAM1" in
"")
help
;;
--list)
echo "Trashcan's content"
cd $TRASH
find *
;;
--delete-older-than)
echo "Delete the files older than $PARAM2 days"
find $TRASH -mtime +$PARAM2 | xargs rm -rf
;;
--search)
echo "Search $PARAM2 among the trashcan's files"
cd $TRASH
find -name *$PARAM2*
;;
--recover)
echo "Recover the file/directory in the original position"
mkdir -p $(dirname $PARAM2)
mv $TRASH$PARAM2 $PARAM2
;;
*)
echo "Security delete a file/directory"
delete
;;
esac
exit 0