Link to home
Start Free TrialLog in
Avatar of AXISHK
AXISHK

asked on

Centos Script

I have difficult to understand the script below. Can any explain the logic ? Tks



#!/bin/bash

PATH1=$1
PATH2=$2
PATH3=$3
echo $! > /var/run/MonitorWait.pid
/usr/bin/inotifywait --timefmt "%c" -mrq -e MODIFY,ATTRIB,CREATE,DELETE --format "%T|%e|%w%f|" $PATH1 $PATH2 $PATH3 | while IFS=\| read TIME EVENT FILE
do

if [[ `expr match $FILE $PATH1*` -gt 0 ]]; then
 PROJECTID=1
elif [[ `expr match $FILE $PATH2*` -gt 0 ]]; then
 PROJECTID=2
elif [[ `expr match $FILE $PATH3*` -gt 0 ]]; then
 PROJECTID=3
fi

### Arg: 1-project id, 2-event ,3-file
php /home/webapps/Monitoring/Process.php $PROJECTID $EVENT $FILE
echo $TIME,$PROJECTID,$EVENT,$FILE >> /home/Monitoring/Monitor.out
Avatar of Tintin
Tintin

The first part monitors (via inotifywait) the 3 paths specified as arguments to the script for any changes (ie: modification, creation, deleting or attribute changes) to files.

The "if" block determines which of the three paths where the change occured and then calls the Process.php script with the appropriate PROJECTID, the changed file and the type of change.
Avatar of AXISHK

ASKER

What does 'while IFS=\|' and '-mrq' mean ?

Thanks
Avatar of AXISHK

ASKER

One more check, what's the meaning of echo $! ?

Great thanks
IFS is the Internal Field Separator.

By default, this is set to whitespace.

So if you have a file containing

one two
three four

Then you can read the contents with a while loop, eg:

while read f1 f2
do
   ..
done <file

Open in new window


However, if the fields use a different separator like the | symbol, eg:

one|two
three|four

Setting IFS to the field separator means you can read them into variables like the previous while loop.

while IFS='|' read f1 f2
do
   ..
done

Open in new window


Note that
IFS='|'

Open in new window


and
IFS=\|

Open in new window


do the same thing.


$! is a built in shell variable that contains the process ID (PID) of the parent process (PPID).
Avatar of AXISHK

ASKER

One last check, does it mean the the output of 'inotifywait' will be read into variable TIME, EVENT and File variable accordingly based on the field separator '\' ?

Thanks
Correct.
SOLUTION
Avatar of skullnobrains
skullnobrains

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AXISHK

ASKER

Tks