0

I am trying to make a bash script with inotiy-tools that will monitor a directory.

Here is my script

while f=$(inotifywait -m -e create -q -r "/media/data2/Music/English"  --format '%f %w')
do
    echo "$f"
done

The problem is when I run the above script it prints nothing on the terminal. I have checked the inotifywait command and it runs fine on terminal but why it is not working inside my script.

inotifywait on terminal

noob@noob:~$ inotifywait -m -e create -q -r /media/data2/Music/English  --format '%f %w'
hello /media/data2/Music/English/
RanRag
  • 48,359
  • 38
  • 114
  • 167

2 Answers2

2

The problem is f=$(inotifywait... waits for that command to end and only then gives you the output.

I rarely write bash, but you could try:

inotifywait .... |
while read line
do
    echo $line
done
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Also can you point me to correct documentation which states that `$()` waits for that command to end and only then gives the output. – RanRag Jul 09 '12 at 11:10
0

Don't use the -m switch in that context, otherwise the inotifywait command will never return the control to the while loop.

tupiniquim
  • 401
  • 4
  • 5