I want to capture inotify CLOSE_WRITE events in a directory and write CSV info to a history file.
My attempt :
#!/bin/bash
dir=/home/stephen/temp
host=$(hostname)
inotifywait -e close_write -m --timefmt '%Y-%m-%d,%H:%M' --format '%T,$host,%e,%w,%f' $dir | while read event;
do
echo $event
done
This produces
2022-06-14,16:58, `hostname`, CLOSE_WRITE,CLOSE, /home/stephen/temp/, testfile.txt
I've tried $host
and $(hostname)
with the same effect. The event's format definition does not accept externally defined variables.
I could wrap it all up in a python script but I'd rather find a shell native solution.