Linux – Running a script containing find with a regexp fails in cron but works in a shell

cronfindlinuxscripting

I have the following script in my /etc/cron.d:

19 15 * * * root /opt/scripts/clean-nexus-release-repo.sh

The clean-nexus-release-repo.sh script looks like this:

#!/bin/bash
find /opt/sonatype-work/nexus/storage/releases/se/company* -regextype posix-extended -depth -regex '.*/r?[0-9]{5,7}[a-Z0-9_.-]*\.[0-9]{1,3}' -mtime +60 -type d -print -exec rm -r {} \;
find /opt/sonatype-work/nexus/storage/releases/nu/company -regextype posix-extended -depth -regex '.*/r?[0-9]{5,7}[a-Z0-9_.-]*\.[0-9]{1,3}' -mtime +60 -type d -print -exec rm -r {} \;

When I run the clean-nexus-release-repo.sh script from my shell everything works fine. When it is run through cron I get the following output:

find: Invalid range end
find: Invalid range end

This is the same error I get when I move the hyphen in the [a-Z0-9_.-] regular expression to an invalid position. I have tried the following but I still get the same error:

  • Specifying an absolute path to the find command
  • Escaping the hyphen with a backslash
  • Removing that hyphen

find --version gives:

find (GNU findutils) 4.4.2

Running on Linux devtools01 2.6.32-71.el6.x86_64 (CentOS Linux release 6.0)

Best Answer

I'm not sure I can give the perfect technical explanation here. maybe someone else can improve the answer.

basically I remember range expressions being locale dependent, so [a-Z] does not necessarily mean the same thing as [a-zA-Z]

I think the relevant documentation is:

http://www.gnu.org/software/grep/manual/grep.html#index-range-expression-216:

Within a bracket expression, a range expression consists of two characters separated by a hyphen. It matches any single character that sorts between the two characters, inclusive, using the locale's collating sequence and character set. For example, in the default C locale, ‘[a-d]’ is equivalent to ‘[abcd]’. Many locales sort characters in dictionary order, and in these locales ‘[a-d]’ is typically not equivalent to ‘[abcd]’; it might be equivalent to ‘[aBbCcDd]’, for example. To obtain the traditional interpretation of bracket expressions, you can use the ‘C’ locale by setting the LC_ALL environment variable to the value ‘C’.

Related Topic