Unix/Linux Go Back    


Shell Programming and Scripting 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

Shell Programming and Scripting


Tags
bash, inotify

Reply    
 
Thread Tools Search this Thread Display Modes
    #15  
Old Unix and Linux 04-09-2018   -   Original Discussion by pasaico
pasaico's Unix or Linux Image
pasaico pasaico is offline
Registered User
 
Join Date: Dec 2013
Last Activity: 12 April 2018, 9:53 AM EDT
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by apmcd47 View Post
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..
Sponsored Links
    #16  
Old Unix and Linux 04-09-2018   -   Original Discussion by pasaico
apmcd47's Unix or Linux Image
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 22 May 2018, 4:28 PM EDT
Posts: 341
Thanks: 16
Thanked 94 Times in 89 Posts
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
Sponsored Links
    #17  
Old Unix and Linux 04-10-2018   -   Original Discussion by pasaico
pasaico's Unix or Linux Image
pasaico pasaico is offline
Registered User
 
Join Date: Dec 2013
Last Activity: 12 April 2018, 9:53 AM EDT
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by apmcd47 View Post
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 Unix and Linux 04-10-2018   -   Original Discussion by pasaico
apmcd47's Unix or Linux Image
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 22 May 2018, 4:28 PM EDT
Posts: 341
Thanks: 16
Thanked 94 Times in 89 Posts
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)
Sponsored Links
    #19  
Old Unix and Linux 04-10-2018   -   Original Discussion by pasaico
pasaico's Unix or Linux Image
pasaico pasaico is offline
Registered User
 
Join Date: Dec 2013
Last Activity: 12 April 2018, 9:53 AM EDT
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by apmcd47 View Post
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
Sponsored Links
    #20  
Old Unix and Linux 04-10-2018   -   Original Discussion by pasaico
apmcd47's Unix or Linux Image
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 22 May 2018, 4:28 PM EDT
Posts: 341
Thanks: 16
Thanked 94 Times in 89 Posts
Quote:
Originally Posted by pasaico View Post
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)
Sponsored Links
    #21  
Old Unix and Linux 04-10-2018   -   Original Discussion by pasaico
Peasant's Unix or Linux Image
Peasant Peasant is offline Forum Advisor  
Registered User
 
Join Date: Mar 2011
Last Activity: 22 May 2018, 1:38 PM EDT
Posts: 1,174
Thanks: 32
Thanked 355 Times in 306 Posts
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.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Dynamically add paths to inotify anil510 UNIX for Advanced & Expert Users 2 01-13-2015 07:40 AM
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 ranga27 Shell Programming and Scripting 3 09-16-2007 12:19 PM



All times are GMT -4. The time now is 07:37 PM.