Bash: Execute script on file save?
I'd like to use Bash to run a test suite automatically when I save any file in a given directory.
Is there a mechanism for bash to execute a given script on save events?
Tha开发者_开发百科nks.
::EDIT::
I should have mentioned that I'm on using OSX.
Edited: you (the OP) mentioned you use OSX. I'm not aware of any similar tools on OSX. There is a low-level system call (inherited from BSD) called "kqueue", but you'd have to implement your own user-level tool. There is a sample application from Apple, called "Watcher", but it's proof of concept only, and doesn't do what you want.
There is another thread about this on Stack Overflow (also inconclusive).
For lack of an appropriate tool, if you're using a specific programming language, I'd advise you to look for solutions already written for it. Otherwise, I think you're stuck to polling and managing the changes yourself...
Here's my original, Linux-based answer, for archival purposes:
If you're using Linux, you might want to take a look at inotify . More specifically, you can install inotify-tools, which include inotifywait
.
With it, you can monitor files and directories for a number of events, such as access, modification, opening, closing and many others. inotifywait
can exit once the specified event has been detected, and so a simple loop would get you what you want:
while :; do
inotifywait -e modify /some/directory
run_test_suite
done
By the way, many programming languages and environments already have their own continuous test runners (for instance, with Python you could use tdaemon, among others).
You can use incron to detect when a file has been closed.
Use dnotify only if inotify is not available in Your system (linux kernel < 2.6.13).
dnotify is standard linux method to watch directories:
http://en.wikipedia.org/wiki/Dnotify
Code example. Watch dir for changes:
dnotify WATCH_DIR -M -e SCRIPT_TO_EXECUTE
Please note SCRIPT_TO_EXECUTE will be executed every time, when any file in WATCH_DIR changes.
You can use inotify as explained here Inotify Example: Introduction to Inotify with a C Program Example
精彩评论