Search code examples
bashwhile-loopinotify

bash script: meaning of while read in a simple script that use inotifywait



#!/bin/sh
inotifywait -qme CREATE --format '%w%f' /tmp/watch | while read f; 
do 
ln -s "$f" /tmp/other/; 
done

I've found this script looking for something that react to filesystem event doing a specific job. The script works perfectly what i don't understand is the meaning of while read f;


Solution

  • It's capturing the output of your inotifywait command and parsing it line by line, assigning each line in turn to f in the while statement.

    That particular inotifywait command continuously monitors the /tmp/watch directory and outputs the full path name when an entry is created.

    The while loop then processes each each of those file names and creates a symbolic link to it in the /tmp/other/ directory.

    Here's an example script showing the while read in action:

    pax> printf "1\n2\n3 4\n" | while read f ; do echo "[$f]" ; done
    [1]
    [2]
    [3 4]