Travis Travis - 1 month ago 7
Bash Question

Incrontab not processing IF statement

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

incrontab
to watch a directory on an Oracle Linux server.

I am trying to modify it so that if it sees file
F078311
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
/export
directory. and
$1
would be
/export/F073811


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


Shell script:

#!/bin/bash
# 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
filename="A$newdate.txt"

joblog="/logs/joblog.log"


[[ ( $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

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)

Comments