我有一个脚本,在查找命令中有一个错误:
echo "find ${logPath} -mtime +${cutOffDays} -type f -name ${dateInNameLogs}.log -depth 1 -print -delete 2>&1" >>$logFile
rslt=$(find ${logPath} -mtime +${cutOffDays} -type f -name ${dateInNameLogs}.log -depth 1 -print -delete 2>&1)
日志文件显示:
find /Users/craig/Desktop/logs -mtime +21 -type f -name *[0-9][0-9][0-9][0-9][-_][0-9][0-9][-_][0-9][0-9].log -depth 1 -print -delete 2>&1
rslt = find: 1: unknown primary or operator
如果我在终端中复制显示并执行的查找命令,则可以正常工作。所以我的rslt = $(... line。
我缺少什么?
谢谢。
引用您的扩展:
rslt=$(find "${logPath}"
-mtime "+${cutOffDays}"
-type f
-name "${dateInNameLogs}.log"
-depth 1
-print
-delete
2>&1)
引用 "${dateInNameLogs}.log"
(以双引号为单词)告诉外壳扩展变量,但 not 将其作为地球范围扩展(或通过单词分配),从而确保值由find
评估,而不是由您的外壳评估。
这使行为鲁棒即使设置了nullglob
,failglob
或类似选项。
事实证明,这是由'shopt -s nullglob'引起的。我的脚本不止一件事,我将其设置在文件的顶部。我已经将其专门移至需要的位置,现在脚本运行良好。