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

后端 未结 6 1161
天涯浪人
天涯浪人 2020-12-14 09:16

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?

6条回答
  •  醉酒成梦
    2020-12-14 09:58

    See this example as an improvement upon on Ian Vaughan's answer:

    #!/usr/bin/env bash
    # script:  watch
    # author:  Mike Smullin 
    # license: GPLv3
    # description:
    #   watches the given path for changes
    #   and executes a given command when changes occur
    # usage:
    #   watch  
    #
    
    path=$1
    shift
    cmd=$*
    sha=0
    update_sha() {
      sha=`ls -lR --time-style=full-iso $path | sha1sum`
    }
    update_sha
    previous_sha=$sha
    build() {
      echo -en " building...\n\n"
      $cmd
      echo -en "\n--> resumed watching."
    }
    compare() {
      update_sha
      if [[ $sha != $previous_sha ]] ; then
        echo -n "change detected,"
        build
        previous_sha=$sha
      else
        echo -n .
      fi
    }
    trap build SIGINT
    trap exit SIGQUIT
    
    echo -e  "--> Press Ctrl+C to force build, Ctrl+\\ to exit."
    echo -en "--> watching \"$path\"."
    while true; do
      compare
      sleep 1
    done
    

提交回复
热议问题