我写了一个find命令,找到了这些文件,但是排除了其他的 files/directories.,我确实回复了这段代码并复制了它。如果我将它粘贴在终端中,它可以工作。有些文件被排除在外。但是,如果我从脚本执行它,它不会按预期工作。

我尽力而为,但它没有解决我的问题。我试图在$()${}这样的括号之间转义我的变量,引用它们 但没有任何效果。

我的查找代码如下所示:

find ${StartDirs[*]} $pat -print

事实上将执行如下:

find ./bin  -wholename './bin/backup2' -prune -o -wholename './bin/backup3' -prune -o -print

上面的第二个代码在终端中工作,但不在脚本中。 我做错了什么?

有关详细信息,我将尝试在下面粘贴必要的代码 我想制作一个backup,并希望用find和cp做到这一点。我的脚本的大多数代码都被省略了。我认为下面的代码是这个问题的必要最小代码。

StartDirs=();
ExcludedFiles=(); #files/directories which needs to be excluded

#check and store excluded files
CheckExcludedFile(){ #this function will be called over and over again by another function (getopts). It depends on the chain of -x options. With the -x option I can decide which file i want to exclude. The Getopts function is also omitted.

    exclFile=`find $1 2>/dev/null | wc -l` 
    if [ $exclFile -lt 1 ]; then
        echo $FILEMAPNOTEXIST | sed 's~-~'$1'~g' # FILEMAPNOTEXIST is a variable from another script with error messages
        exit 0
    else
        ExcludedFiles+=($1) #add excluded file/dir path to array
    fi
}

MakeBackup(){
        for i in ${ExcludedFiles[*]}
        do
            s=" -wholename $i -prune -o"
            pat=$pat$s
        done
        # the code above edits the array elements of the EcludedFIles[]
        #For example by calling the script with the -x option (-x fileA -x fileB -x fileC) wordt als volgt: -wholename 'fileA' -prune -o -wholename 'fileB' -prune -o -wholename 'fileC' -prune -o.
        #the pat variable will be used for the find command to ignore files/directories

    mkdir -p ~/var

    echo "Start-time $(date '+%F %T')" >> ~/var/dq.log  

    find ./bin  -wholename './bin/backup2' -prune -o -wholename './bin/backup3' -prune -o -print
    #the code above should work like in terminal. That is not the case..

    # find ${StartDirs[*]} $pat -print #this should work also.

    # cp -av ${StartDirs[@]} $Destination >> ~/var/dq.log find command not working therefore this rule is commented

    echo "end-time $(date '+%F %T')" >> ~/var/dq.log
}

请告诉我解决方案,我真的卡住了。 提前致谢!

如果给出,预期结果应该只是某些 files/directories被排除在外。

如果需要完整的脚本让我知道

分析解答

命令find ./bin -wholename './bin/backup2' -prune -o -wholename './bin/backup3' -prune -o -print应按预期工作,如果当前目录直接在bin/上方。这可能是您的问题的原因:如果在实际脚本中您组装的路径名与找到的路径中的前缀不匹配,那么例如,修剪不起作用。示例:您有一个目录/home/me;在它是bin/backup2/bin/backup3/stuff-to-backup/。现在,如果你在/home/me并执行find .,它会找到例如./bin/backup2将被修剪。

但是如果你把它放在脚本中并使用路径参数调用脚本,例如/home/me,它会找到相同的文件,但路径会有所不同,例如/home/me/bin/backup2,并且不会修剪它,因为它与提供的排除模式不匹配,即使它们是相同的文件。同样不会找到-wholename提供的模式。 这里是一个解决此问题的问题。