Travis Travis - 1 year ago 71
Bash Question

Incrontab not processing IF statement

First off I'm a Windows guy but getting tasked with Linux now. I am using

to watch a directory on an Oracle Linux server.

I am trying to modify it so that if it sees file
it will echo Test to the log file. Otherwise it will check to see if the file matches the 2nd format and executes that echo.

It is running the 2nd IF and echo'ing the date to the log but is not echoing the Test to the log. Is there something wrong with my syntax?

It is watching the
directory. and
would be

incrontab -l
/export IN_MODIFY,IN_CLOSE_WRITE,IN_MOVED_TO /usr/local/scripts/ $@/$#

Shell script:

# This file is invoked using incron and will rename the file with the current date.

# Set newdate as current date, YYYYMMDD-HHMMSS
newdate="$(date +%Y%m%d-%H%M%S)"

#Sets the filename as the filename_YYYYMMDD-HHMMSS


[[ ( $1 =~ F073811$ ) ]] && echo Test >> /logs/joblog.log

#checks to file format to see if it looks like name_YYYYMMDD-HHMMSS. If it does not then it renames the file to that format.
[[ ! ( $1 =~ A[0-9]{8}-[0-9]{6}\.txt$ ) ]] && echo ---- `date` ------ >> "$joblog"

Answer Source

In that case, no need for regex, use basename to compute exact filename without path, then just check for equality

[[ $(basename $1) = F073811 ]] &&  echo Test >> /logs/joblog.log

Note: I just noticed that your command line works for me as-is, but not in Bash 3, only in Bash 4. The above method works for both and doesn't involve regexes, plus it won't match /export/XXXXF073811 whereas your regex would.

The more I think of it the more I'm positive that the problem could very well be that you're called with /export/F073811\r as first argument (MS-DOS line termination char slipped in, very frequent on Linux)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download