【发布时间】:2011-02-27 16:51:49
【问题描述】:
我想在文件更改时自动启动构建。
我在 Ruby 中使用过 autospec (RSpec) 并喜欢它。
如何在 bash 中做到这一点?
【问题讨论】:
标签: linux bash build automation rspec
我想在文件更改时自动启动构建。
我在 Ruby 中使用过 autospec (RSpec) 并喜欢它。
如何在 bash 中做到这一点?
【问题讨论】:
标签: linux bash build automation rspec
【讨论】:
关键字是 inotifywait 和 inotifywatch 命令
【讨论】:
在阅读了其他帖子的回复后,我找到了一个帖子(现已消失),我创建了这个脚本:-
#!/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
【讨论】:
这个脚本怎么样?使用“stat”命令获取文件的访问时间,并在访问时间发生变化时(无论何时访问文件)运行命令。
#!/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
【讨论】:
请参阅 this 示例作为对 Ian Vaughan 答案的改进:
#!/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
【讨论】:
如果你已经安装了entr,那么在shell中你可以使用以下语法:
while true; do find src/ | entr -d make build; done
【讨论】: