2 ms·
Thanks. Below is the version I'm trying. It allows multiple-argument commands, doesn't stomp on atchange if inotifywait isn't installed and I wanted it to fire
by imurray 16y ago
Thanks. Below is the version I'm trying. It allows multiple-argument commands, doesn't stomp on atchange if inotifywait isn't installed and I wanted it to fire when touching files as well as modifying them.
if which inotifywait > /dev/null ; then
function atchange(){
FILE="$1"
shift
while inotifywait -qq -e modify -e attrib "$FILE"; do
"$@";
done;}
fi
Looking again at atchange it deliberately waits after first noticing the file has been modified, in case something is still modifying it. It fires once writing has stopped for at least a second. I'm not sure if I need that in my use cases, but its authors clearly did so I'm pointing out that the above shell function does not provide a true like-for-like replacement. (Also atchange allows a single argument to be a command_file, functionality I never use.)
EDIT changed the "while true" so that the function stops on an error rather than infinitely looping when something goes wrong, like FILE ceasing to exist. A file temporarily disappearing would scupper the script though, whereas the perl script deals quietly with that.