0

我已经制作了这个脚本,但是我很难为这种情况编写代码:在垃圾桶里不应该有同名的文件;在这种情况下,它们应该被重命名。

我该如何解决?

这是我的代码:

#!/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
4

3 回答 3

0

Quick and dirty solution:

if [[ -f $TRASH$PARAM ]]; then
    mv "$PARAM1" "$TRASH$PARAM$RANDOM$RANDOM" # file exists
else
    mv "$PARAM1" "$TRASH$PARAM" # ok, it is fine, file does not exist
fi

Also please note that you have to quote every variable in your script when it is passed as a parameter.

  • if [ ${PARAM1:0:1} = "/" ]; then must be changed to if [ "${PARAM1:0:1}" = "/" ]; then or even better if [[ ${PARAM1:0:1} = "/" ]]; then
  • mkdir -p $(dirname $TRASH$PARAM1) to mkdir -p "$(dirname "$TRASH$PARAM1")"
    And so on...
于 2013-08-27T21:54:46.947 回答
0

实际上,我是在 2010 年写的。现在有一个用于 Ubuntu 的垃圾 cli 包。

http://wiki.linuxquestions.org/wiki/Scripting#Command_Line_Trash_Can

于 2014-01-16T22:46:43.043 回答
0

当您的文件存储在垃圾桶中时,请考虑在您的文件上生成一个尾随总和(如 sha1sum),以防止与类似文件发生冲突。例如

$HOME/.Trash/home/user/same/path/same_name.9ce1f394b955306f7c450cbf0d96d2f17f6a1394
$HOME/.Trash/home/user/same/path/same_name.b0dc31b1919c02932892b59d0c0e365cd75629c6

恢复这些文件时,您刚刚删除了总和,例如

/home/user/same/path/same_name

该解决方案还可以防止真正相同的文件的重复,因为 sum 可以做的可能是唯一的。

如果您足够信任总和,您也可以选择不将目录存储在垃圾箱中。只是他们的签名,上面有一个额外的信息文件,比如:

$HOME/.Trash/same_name.9ce1f394b955306f7c450cbf0d96d2f17f6a1394
$HOME/.Trash/same_name.9ce1f394b955306f7c450cbf0d96d2f17f6a1394.info

其中 info 包含文件所在目录的绝对路径。

/home/user/same/path/

您甚至可以在其上添加其他属性,例如目录权限等。

[File]
/home/user/same/path/same_name

[Attributes]
/home[ TAB ]USER:GROUP[ TAB ]0755
/home/user[ TAB ]USER:GROUP[ TAB ]0755
/home/user/same[ TAB ]USER:GROUP[ TAB ]0755
/home/user/same/path[ TAB ]USER:GROUP[ TAB ]0755
/home/user/same/path/same_name[ TAB ]USER:GROUP[ TAB ]0644

基本上总和只是概念,但是您可以在自己的基础上添加更多技巧,以使垃圾中的文件存在更加确定是唯一的,只是您必须考虑它不能再阻止具有相同的文件作为两个条目存在,这本来是不必要的。

当然,如果你想在你的脚本中支持带空格和喜欢的文件名,请始终将变量放在双引号内以防止它们分词,这意味着它们以后可能会被解释为命令的两个或多个参数,从而导致语法错误命令或意外结果,其中一些可能是不可撤销的。

do something "$var" "${etc}xyz"
于 2013-08-27T23:32:38.643 回答