监视一组文件的更改并在更改时对其执行命令

发布于 2024-07-11 00:29:42 字数 487 浏览 13 评论 0原文

我想到的(命令行)界面如下所示:

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

但我并不热衷于该界面。 我将添加我的脚本来作为答案,看看是否有人有更好的东西或改进它的方法。

The (command line) interface I have in mind is like so:

watching FILE+ do COMMAND [ARGS] (and COMMAND [ARGS])*

Where any occurrence of "{}" in COMMAND is replaced with the name of the file that changed. And note that "do" and "and" are keywords.

For example:

> watching foo.txt bar.txt do scp {} somewhere.com:. and echo moved {} to somewhere

Or:

> watching foo.c do gcc foo.c and ./a.out

I'm not wedded to that interface though. I'll add my script that does that as an answer and see if anyone has anything better or ways to improve it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悍妇囚夫 2024-07-18 00:29:42
#!/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;
}
#!/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;
}
南七夏 2024-07-18 00:29:42

您可以使用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

You can use entr, e.g.

ls foo.txt bar.txt | entr sh -c 'rsync -vuar *.txt somewhere.com:. && echo Updated'

Unfortunately you can't check which file has been changed, but using rsync -u will automatically skip files which haven't been changed.

Here is the second example:

ls foo.c | entr sh -c 'gcc foo.c && ./a.out'

or simply use make, e.g.

ls -d * | entr sh -c 'make && make test'

To watch the directory for changes, use -d parameter wrapped inside a shell loop, e.g.:

while true; do find path/ | entr -d do_stuff; done
戏蝶舞 2024-07-18 00:29:42

请检查inotify工具

Please check the inotify tools.

梦明 2024-07-18 00:29:42

我刚刚发现这个用 perl 编写的 every_change 脚本,它与我在答案中发布的一个。

该脚本非常适合代码开发。 监视文件并在每次更改时运行它(或其他内容)。 在一个窗口中编写代码,然后观察它在另一个窗口中自动执行。

所以基本上每次文件更改时它都会执行一些操作。

I just found this every_change script written in perl, that's very similar to the one I posted in my answer.

This script kicks ass for code development. Watches a file and runs it (or something else) every time it changes. Write your code in one window, and watch it automatically execute in another.

So basically it does something every time a file changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文