👤
Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Hotfolder with inotify-tools, loop FOR not working

Tags
bash, inotify

👤 Login to reply

 
Thread Tools Search this Thread
# 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 10: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 05: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
The Following User Says Thank You to apmcd47 For This Useful Post:
pasaico (04-10-2018)
# 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
The Following User Says Thank You to apmcd47 For This Useful Post:
pasaico (04-10-2018)
# 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 to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
For-loop not working API Shell Programming and Scripting 4 07-29-2015 09:50 AM
Dynamically add paths to inotify anil510 UNIX for Advanced & Expert Users 2 01-13-2015 07:40 AM
for loop not working samnyc UNIX for Dummies Questions & Answers 3 01-09-2012 04:30 PM
inotify() in SCO UNIX javad1_maroofi SCO 1 08-08-2010 09:20 AM
While loop not working premkumardr Shell Programming and Scripting 5 06-15-2010 02:34 AM
If loop is not working toanilsharma1 UNIX for Advanced & Expert Users 12 08-20-2008 12:21 PM
If then else loop not working findprakash UNIX for Dummies Questions & Answers 2 05-25-2008 12:43 AM
if loop is not working ranga27 Shell Programming and Scripting 3 09-16-2007 12:19 PM
Loop not working nitin Shell Programming and Scripting 2 11-07-2001 06:55 AM


All times are GMT -4. The time now is 11:03 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?