I am trying to create a bash script with 2 parameters:
I want to watch the directory parameter for changes: when something has been changed the script should execute the command.
I'm running MacOS, not Linux; any pointers or external resources would greatly help as I have see that this is difficult to achieve. Really OI am trying to mimic SASS's watch functionality.
#!/bin/bash #./watch.sh $PATH $COMMAND DIR=$1 ls -l $DIR > $DIR/.begin #this does not work DIFFERENCE=$(diff .begin .end) if [ $DIFFERENCE = '\n']; then #files are same else $2 fi ls -l $DIR > $DIR/.end
In Linux, we can use the inotify interface to monitor a directory or a file. We do this by adding a watch to the directory or file. When we add a watch to a file, we can monitor it. For example, we'll know when a process opens, modifies, reads closes, moves, or deletes the file.
Basic Bash Commandscd path-to-directory : The command followed by a path allows you to change into a specified directory (such as a directory named documents ). cd .. (two dots). The .. means “the parent directory” of your current directory, so you can use cd .. to go back (or up) one directory.
In order to run a Bash script from anywhere on your system, you need to add your script to your PATH environment variable. Now that the path to the script is added to PATH, you can call it from where you want on your system. $ script This is the output from script!
To continuously recursively monitor folder (md5) and execute a command on change:
daemon() { chsum1="" while [[ true ]] do chsum2=`find src/ -type f -exec md5 {} \;` if [[ $chsum1 != $chsum2 ]] ; then if [ -n "$chsum1" ]; then compile fi chsum1=$chsum2 fi sleep 2 done }
Works on my OS X as I do not have digest
.
On Linux, you can use md5sum
as a replacement for the md5
command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With