Ian Vaughan Ian Vaughan - 5 months ago 8
Linux Question

linux script that monitors file changes within folders (like autospec does!)

I want to automatically kick off a build whenever a file changes.

I've used autospec (RSpec) in Ruby and loved that.

How can this be done in bash?

Answer

After reading replies to other posts, I found a post (now gone), I created this script :-

#!/bin/bash

sha=0
previous_sha=0

update_sha()
{
    sha=`ls -lR . | sha1sum`
}

build () {
    ## Build/make commands here
    echo
    echo "--> Monitor: Monitoring filesystem... (Press enter to force a build/update)"
}

changed () {
    echo "--> Monitor: Files changed, Building..."
    build
    previous_sha=$sha
}

compare () {
    update_sha
    if [[ $sha != $previous_sha ]] ; then changed; fi
}

run () {
    while true; do

        compare

        read -s -t 1 && (
            echo "--> Monitor: Forced Update..."
            build
        )

    done
}

echo "--> Monitor: Init..."
echo "--> Monitor: Monitoring filesystem... (Press enter to force a build/update)"
run