我已经制作了这个脚本,但是我很难为这种情况编写代码: 在垃圾桶中应该没有相同名称的文件;在这种情况下,他们应该重新命名。脚本bash外壳 - 制作替代垃圾桶
我该如何解决?
这里是我的代码:
#!/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
你仍然需要获取该文件的路径名的绝对形式,如果一个相对路径传递,即转换..和。条目正确,因为这些条目不能被创建为目录。尝试使用'realpath'或'readlink -m'。您也可以使用其他绝对路径解决方案:http://www.linuxquestions.org/questions/blog/konsolebox-210384/getting-absolute-paths-of-unix-directories-and-filenames-in-shell-scripts-3956 /。即使路径以/开头,你也不能确定它们是绝对形式,可能包含'.'或'..'。 – konsolebox
你可能会在https://github.com/robrwo/bashtrash – Rob