Reputation: 385
I have a script thats gets an error in a find command:
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)
The log file shows:
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
If I copy the find command displayed and execute in terminal, it works fine. So something is wrong with my rslt=$(... line.
What am I missing ?
Thanks.
Upvotes: 0
Views: 189
Reputation: 295914
Quote your expansions:
rslt=$(find "${logPath}" \
-mtime "+${cutOffDays}" \
-type f \
-name "${dateInNameLogs}.log" \
-depth 1 \
-print \
-delete \
2>&1)
Quoting "${dateInNameLogs}.log"
(in double quotes) tells the shell to expand the variable, but not to expand the result of that as a glob (or to put it through word splitting), thus ensuring that the value is evaluated by find
, not by your shell.
This makes behavior robust even if nullglob
, failglob
, or similar options are set.
Upvotes: 3
Reputation: 385
Turns out it was caused by 'shopt -s nullglob'. My script does more than one thing and I had that set at the top of the file. I've moved it to specifically where is was needed and now the script runs fine.
Upvotes: 0