4

私が念頭に置いている(コマンドライン)インターフェイスは次のようなものです。

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

しかし、私はそのインターフェースに縛られていません。それを行うスクリプトを答えとして追加し、誰かがそれを改善するためのより良いものや方法を持っているかどうかを確認します。

4

4 に答える 4

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;
}
于 2008-12-25T20:49:18.627 に答える
1

every_changeperlで書かれたこのスクリプトを見つけました。これは、回答に投稿したものと非常によく似ています。

このスクリプトは、コード開発に最適です。ファイルを監視し、変更されるたびにそれ (または何か他のもの) を実行します。1 つのウィンドウでコードを記述し、別のウィンドウで自動的に実行されるのを確認します。

したがって、基本的には、ファイルが変更されるたびに何かを行います。

于 2008-12-26T22:03:40.083 に答える
1

inotifyツールを確認してください。

于 2008-12-26T20:35:36.577 に答える