Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

Thanks.

::EDIT::

I should have mentioned that I'm on using OSX.

like image 256
Thomas Avatar asked Jul 19 '10 17:07

Thomas


People also ask

How do I save and run a shell script?

To save a file, you must first be in Command mode. Press Esc to enter Command mode, and then type :wq to write and quit the file. The other, quicker option is to use the keyboard shortcut ZZ to write and quit.


1 Answers

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).

like image 101
rbp Avatar answered Sep 25 '22 13:09

rbp