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

后端 未结 6 1154
天涯浪人
天涯浪人 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:53

    If you've entr installed, then in shell you can use the following syntax:

    while true; do find src/ | entr -d make build; done
    
    0 讨论(0)
  • 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 <mike@smullindesign.com>
    # license: GPLv3
    # description:
    #   watches the given path for changes
    #   and executes a given command when changes occur
    # usage:
    #   watch <path> <cmd...>
    #
    
    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
    
    0 讨论(0)
  • 2020-12-14 10:06

    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
    
    0 讨论(0)
  • 2020-12-14 10:09

    keywords are inotifywait & inotifywatch commands

    0 讨论(0)
  • 2020-12-14 10:10

    How about this script? Uses the 'stat' command to get the access time of a file and runs a command whenever there is a change in the access time (whenever file is accessed).

    #!/bin/bash
    while true
    do
       ATIME=`stat -c %Z /path/to/the/file.txt`
       if [[ "$ATIME" != "$LTIME" ]]
       then
           echo "RUN COMMNAD"
           LTIME=$ATIME
       fi
       sleep 5
    done
    
    0 讨论(0)
  • 2020-12-14 10:12

    Take a look at incron and inotify-tools.

    0 讨论(0)
提交回复
热议问题