Question

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/
Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top