SciGuyMcQ
SciGuyMcQ

Reputation: 1043

Ubuntu BASH inotifywait to trigger another script

I am trying to use inotifywait within a bash script to monitor a directory for a file with a certain tag in it (*SDS.csv).

I also only want to execute once (once when the file is written to the directory data ).

example:

#! /bin/bash
inotifywait -m -e /home/adam/data | while read LINE
do
  if [[ $LINE == *SDS.csv ]]; then
     ./another_script.sh
  fi
done

Upvotes: 0

Views: 224

Answers (1)

ash
ash

Reputation: 5155

While this may not be the ideal solution, it may do the trick:

#! /bin/bash
while true
do
    FNAME="$(inotifywait -e close_write /home/adam/data | awk '{ print $NF }')"

    if [ -f "/home/adam/data/$FNAME" ]
    then
      if grep -q 'SDS.csv' "/home/adam/data/$FNAME"
      then
         ./another_script.sh
      fi
    done
done

Upvotes: 0

Related Questions