我编写了一个find命令,该命令可以查找文件,但不包括其他文件/目录.我确实回显了这段代码并复制了它.如果我将其粘贴到终端中,它将起作用.某些文件被排除在外.但是,如果我从脚本中执行它,它将无法按预期工作.
我试图在$()或${}之类的括号之间转义变量,并将其引用,但没有任何效果.
我的查找代码如下所示:
find ${StartDirs[*]} $pat -print
实际上,它将像以下那样执行:
find ./bin -wholename './bin/backup2' -prune -o -wholename './bin/backup3' -prune -o -print
上面的第二个代码在终端中起作用,但在脚本中不起作用.
我做错了什么?
有关更多信息,我将尝试在下面粘贴必要的代码
我正在尝试进行备份,并希望使用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
}
预期结果应该只是排除某些文件/目录(如果给出).
如果需要完整的脚本,请告诉我.
解决方法:
find ./bin -wholename’./bin/backup2′-prune -o -wholename’./bin/backup3′-prune -o -print命令应按预期工作,前提是当前目录位于bin /上方.这可能是造成问题的原因:如果在真实脚本中汇编的路径名与找到的路径中的前缀不匹配,则例如修剪不起作用.示例:您有一个目录/ home / me;其中是bin / backup2 /,bin / backup3 /和东西到备份/.现在,如果您位于/ home / me并执行find.它找到例如./bin/backup2将被修剪.
但是如果您将其放在脚本中并使用路径参数调用脚本,例如/ home / me,它将找到相同的文件,但路径将不同,例如/ home / me / bin / backup2,并且不会修剪它,因为它与提供的排除模式不匹配,即使它们是相同的文件也是如此.同样,找不到与-wholename一起提供的模式. Here是一个解决此问题的问题.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。