我想到的(命令行)界面是这样的:
watching FILE+ do COMMAND [ARGS] (and COMMAND [ARGS])*
{}
中出现的任何“COMMAND
”被替换为更改的文件的名称。请注意,“do
”和“and
”是关键字。
例如:
> watching foo.txt bar.txt do scp {} somewhere.com:. and echo moved {} to somewhere
或者:
> watching foo.c do gcc foo.c and ./a.out
虽然我并不喜欢那个界面。我将添加我的脚本作为答案,看看是否有人有更好的方法或改进方法。
答案 0 :(得分:2)
#!/usr/bin/perl
# Run some commands whenever any of a set of files changes (see USAGE below).
# Example:
# ./watching.pl foo.txt bar.txt do scp foo.txt remote.com:. and cat bar.txt
# To only do something to the file that changed, refer to it as {}.
$| = 1; # autoflush
my $p = position("do", @ARGV); # position of 1st occurrence of "do" in @ARGV.
if (@ARGV < 3 || $p == -1 || !($p >= 1 && $p < $#ARGV)) {
die "USAGE: watching FILE+ do COMMAND [ARGS] (and COMMAND [ARGS])*\n";
}
my $cmdstr = join(' ', splice(@ARGV, $p+1)); # grab stuff after the "do"
my @cmds = split(/\s+and\s+/, $cmdstr);
pop(@ARGV); # remove the "do" on the end.
my @targets = @ARGV;
print "Watching {", join(' ', @targets), "} do (", join('; ', @cmds), "):\n";
# initialize the %last hash for last mod time of each file.
for my $t (@targets) {
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat($t);
$last{$t} = $mtime;
}
my $i = 1;
while(1) {
if($i % (45*60) == 0) { print "."; }
for my $t (@targets) {
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat($t);
if ($mtime != $last{$t}) {
print "\nCHANGE DETECTED TO $t\n";
for (@cmds) { my $tmp = $_; $tmp =~ s/\{\}/$t/g; system($tmp); }
$last{$t} = $mtime;
}
}
sleep(1);
$i++;
}
# Call like so: position($element, @list).
sub position {
my $x = shift;
if(@_==0) { return -1; }
if($x eq $_[0]) { return 0; }
shift;
my $p = position($x,@_);
if($p==-1) { return -1; }
return 1+$p;
}
答案 1 :(得分:1)
请查看inotify
tools。
答案 2 :(得分:1)
我刚刚发现用perl编写的这个every_change
script,这与我在答案中发布的非常相似。
此脚本为代码开发提供了帮助。观察文件并在每次更改时运行它(或其他内容)。在一个窗口中编写代码,然后在另一个窗口中自动执行。
所以基本上每次文件发生变化都会有所作为。
答案 3 :(得分:0)
您可以使用entr
,例如
ls foo.txt bar.txt | entr sh -c 'rsync -vuar *.txt somewhere.com:. && echo Updated'
很遗憾,您无法检查哪些文件已被更改,但使用rsync -u
会自动跳过尚未更改的文件。
以下是第二个例子:
ls foo.c | entr sh -c 'gcc foo.c && ./a.out'
或仅使用make
,例如
ls -d * | entr sh -c 'make && make test'
要查看目录以进行更改,请使用包裹在shell循环中的-d
参数,例如:
while true; do find path/ | entr -d do_stuff; done