Script in bash to do something when a file is modified

81 views Asked by At

I'm trying to make a script to do something when a file is modified. This would work if I run the watcher in another terminal and I edit the file in another vim instance.

However, what I want is that the script opens the vim instance and then run the watcher to do things when I save the file.

This is my script, run.sh:

#!/usr/bin/env bash
vim chart.csv

inotifywait --quiet --recursive --monitor --event modify chart.csv | while read; do
    notify-send "hi, world"
done
1

There are 1 answers

15
Philippe On BEST ANSWER

If you have gnome-terminal, then

#!/usr/bin/env bash
gnome-terminal -e "vim chart.csv"
# Or xterm -e vim chart.csv &

inotifywait --quiet --recursive --monitor --event modify chart.csv | while read; do
    notify-send "hi, world"
done

Update

If you want kill the process when vim quits, try this :

#!/usr/bin/env bash

urxvt -e bash -c "vim chart.csv; kill $$"

inotifywait --quiet --recursive --monitor --event modify chart.csv | while read; do
    notify-send "hi, world"
done