Hotfolder with inotify-tools, loop FOR not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Hotfolder with inotify-tools, loop FOR not working
# 15  
Old 04-09-2018
Quote:
Originally Posted by apmcd47
This is the solution I proposed (now properly coded):
Code:
#!/bin/bash
HOTFOLDER=$PWD/HOTFOLDER2

inotifywait -q -m -e create ${HOTFOLDER} | while read folder event file
do
   if [[ "$event" =~ ^CREATE$ ]] && [[ "${file}" =~ \.xlsx$ ]]
   then
      fullpath="$folder/$file"
      send=${HOTFOLDER}/$(date +'SEND-%F')
      mkdir -p "${send}" && mv "${fullpath}" "${send}"
   fi
done

Now, every time a file is copied or otherwise created the while loop is run. I am checking for a CREATE rather than a CREATE,ISDIR and the file has the correct extension.

Andrew
thank you very much your suggestions have been very useful,

I would like to add the function to check the email from a text file.
If the variable $mail is present, togheter other event (CREATE and .xlsx) proceed with Echo "OK"
otherwise with echo "ERROR"

This way it does not work, I do not understand much about expression and operators

can you help me another time?

Code:
#!/bin/bash 
HOTFOLDER=$PWD/HOTFOLDER

inotifywait -q -m -e create ${HOTFOLDER} | while read folder event file
do

filename="${file##*/}"
name=$(echo "${filename%.*}")
mail=$(awk -F ',' -v var="$name" '$1==var {print $2,$3}' /address.txt)

if [[ "$event" =~ ^CREATE$ ]] && [[ "${file}" =~ \.xlsx$ ]] && [[ "$mail" = '' ]]
        then
                echo "ERROR"

         else
                echo "OK"
         fi
  done


Last edited by pasaico; 04-09-2018 at 11:24 AM..
# 16  
Old 04-09-2018
What does the file at /address.txt contain?
Code:
#!/bin/bash 
HOTFOLDER=$PWD/HOTFOLDER

inotifywait -q -m -e create ${HOTFOLDER} | while read folder event file
do

filename="${file##*/}" #(1)
name=$(echo "${filename%.*}") #(2)
mail=$(awk -F ',' -v var="$name" '$1==var {print $2,$3}' /address.txt)

if [[ "$event" =~ ^CREATE$ ]] && [[ "${file}" =~ \.xlsx$ ]] && [[ "$mail" = '' ]]
        then
                echo "ERROR"

         else
                echo "OK"
         fi
  done

(1) There will be no filepath component, so you do not have to strip it off.
(2) You don't need to put this in an echo:
Code:
name="${filename%.*}"

will work just as well.

Andrew
# 17  
Old 04-10-2018
Quote:
Originally Posted by apmcd47
What does the file at /address.txt contain?

Andrew
address.txt contain:

1,mail@mail.com,mail2@mail.com
2,mail2@mail.com
3,mail3@mail.com,mail4@mail.com


if i put a file 1.xlsx it's check with $mail if present in address.txt and generate "ECHO OK"
if i put 4.xlsx it should generate "ECHO ERROR"

thank you for other suggest

Last edited by pasaico; 04-10-2018 at 06:35 AM..
# 18  
Old 04-10-2018
Presumably, going back to your original script, you want to move the file and email confirmation if the email address(es) exist. What happens if there is no email address? Move anyway? Or is that an error?
Code:
   if [[ "$event" =~ ^CREATE$ ]] && [[ "${file}" =~ \.xlsx$ ]]
   then
      echo "do the copy"
      mail=$(awk -F ',' -v var="${file%.*}" '$1==var {print $2,$3}' ${addr})
      if [[ -n "$mail"  ]]
      then
         echo "Can send email"
      else
         echo "ERROR cannot send email"
      fi
   else
      echo "do nothing"
   fi

Code:
   if [[ "$event" =~ ^CREATE$ ]] && [[ "${file}" =~ \.xlsx$ ]] && [[ -n "$mail"  ]]
   then
      echo "OK"
   else
      echo "ERROR"
   fi

In my opinion, rather than using
Code:
[[ "$var" = '' ]]

it is better to use
Code:
[[ -z "$var" ]]

for string is zero-length or
Code:
[[ -n "$var" ]]

for string is non-zero-length.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 19  
Old 04-10-2018
Quote:
Originally Posted by apmcd47
Presumably, going back to your original script, you want to move the file and email confirmation if the email address(es) exist. What happens if there is no email address? Move anyway? Or is that an error?
If there are no email addresses, the file is moved into a directory named ERROR

Code:
error=${HOTFOLDER}/$(date +'ERROR-%F')
                mkdir -p "${error}" && mv "${fullpath}" "${error}"

why with:

Code:
[[ -n "$mail"  ]] #work
[[ "$mail" = '' ]] #not work?

Thank you Andrew

pasaico
# 20  
Old 04-10-2018
Quote:
Originally Posted by pasaico
why with:

Code:
[[ -n "$mail"  ]] #work
[[ "$mail" = '' ]] #not work?

Thank you Andrew

pasaico
I don't know. It should have.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 21  
Old 04-10-2018
Be careful with inotifywait
If processing inside the loop lasts long, you could miss events.

Try to create a 1000 files using touch and for loop in monitored directory and check if desired result is accomplished by your code.

If such events occur - creating a 1000 files in a short period, your program might not work correctly.
Handling such scenarios will require much more shell code.

There is also a slight problem of someone abusing your watch script by file creation, which can cause real requests will not be processed, unwanted email spam etc.

Hope that helps
Regards
Peasant.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Tip: inotify cron

Dear members, moderators and others. While working on <insert project>, a need has surfaced to watch a directory, and when a file comes, to do appropriate action. So, i started writing some shell code, aware of linux inotify-tools package with inotifywait. Also, i'm seeing a lot of similar... (1 Reply)
Discussion started by: Peasant
1 Replies

2. Red Hat

Issues installing inotify-tools on RedHat Linux

I wish to install inotify-tools-3.20.1-2.4.x86_64.rpm on Linux and fire inotifywait command. Initially i was looking for "inotify-tools-3.xx.tar.gz" as instructed here http://jensd.be/248/linux/use-inotify-tools-on-centos-7-or-rhel-7-to-watch-files-and-directories-for-events I have... (9 Replies)
Discussion started by: mohtashims
9 Replies

3. Shell Programming and Scripting

For-loop not working

Hello all, I would like to unzip some files with a for-loop. Is there anyone who could tell me how I should do this - in a correct way? for file in $(ls); do echo gzip -d < $file | tar xf -; done The problem is the pipe - I believe. But how could I do it? I need it for the command... (4 Replies)
Discussion started by: API
4 Replies

4. UNIX for Advanced & Expert Users

Dynamically add paths to inotify

I have initiated an inotify process with --fromfile option and the file contain the paths to be monitored. /usr/local/maldetect/inotify/inotifywait -d -r -o /usr/local/maldetect/inotify/inotify_log --fromfile /usr/local/maldetect/sess/inotify.paths.28364 --exclude (^/var/tmp/mysql.sock)... (2 Replies)
Discussion started by: anil510
2 Replies

5. Shell Programming and Scripting

while loop not working

hi everyone i am facing a very strange problem . please help me why my while loop is not working. below is my code #!/bin/ksh file=RCMC_ABC_999_080924_210813.DAR echo ${file} value=001 count=10 echo "abc" echo ${count} value=$((${value} + 1 )) echo ${value} while ] do echo... (3 Replies)
Discussion started by: aishsimplesweet
3 Replies

6. SCO

inotify() in SCO UNIX

Hi I'm going to write a program in C language to watch activity on file system in SCO UNIX openserver 5.0.6. Apparently Linux provide an API called "inotify" , I'm wondering what's the SCO UNIX package for this ? (1 Reply)
Discussion started by: javad1_maroofi
1 Replies

7. Shell Programming and Scripting

While loop not working

here we go.. While loop is not working file.. It also invokes one more shell scripts for which parameters need to passed on. while read line do #### #### We want to have a logfile for each load #### PLog="${LogDir}/${Script}.log"; #### ... (5 Replies)
Discussion started by: premkumardr
5 Replies

8. Shell Programming and Scripting

For loop not working...! :(

Could some one help me on this... For loop is working...! for x in $i do for y in $j do z=`echo $y | awk '{print $2}'` if then FS=`/usr/bin/echo $y` echo $FS >>$Basic_location/out.csv fi done CPRT="Cpoyright @ BTOIDCIM" done (3 Replies)
Discussion started by: bullz26
3 Replies

9. Shell Programming and Scripting

if loop is not working

Hello i am trying to remove a line using an input file , but this depends upon user interaction here is the sample #!/bin/sh echo "Please enter whether you want to remove Profile" read value1 if ;then sed /movie/d temp.txt> temp3.txt else echo "Script didnot remove profile" fi ... (3 Replies)
Discussion started by: ranga27
3 Replies
Login or Register to Ask a Question