RanRag
RanRag

Reputation: 49597

Inotifywait not working when using inside bash script

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/

Upvotes: 0

Views: 2750

Answers (2)

tupiniquim
tupiniquim

Reputation: 411

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

Upvotes: 1

cnicutar
cnicutar

Reputation: 182754

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

Upvotes: 2

Related Questions