swatch

ログを監視するのだ.

マニュアルを見ると,

       exec command
           Execute command. The command may contain variables which are sub-
           stituted with fields from the matched line. A $N will be replaced
           by the Nth field in the line. A $0 or $* will be replaced by the
           entire line.

とか書いてるので素直に下記のような設定ファイルを読ませる

$ cat .swatchconf
watchfor        /test/
        exec echo $0

と,こんな感じの動作になる.

$ swatch --config-file=.swatchconf --tail-file=~/swatchtest &
$ echo hogetest >> swatchtest
/home/username/.swatch_script.6059

中間スクリプトのおいてあるパスが出されてもねえ.

適当に探してみると,

"$_"を使うと良いようだ(まあ,perlだものな).

$ cat .swatchconf
watchfor        /test/
        exec echo $0; echo $_
$ swatch --config-file=.swatchconf --tail-file=~/swatchtest &
$ echo hogetest >> swatchtest
/home/username/.swatch_script.6102
hogetest

'--awk-field-syntax' オプションについてはマニュアルで特に何も触れられていないのでソースを探してみる.

#
# convert_command -- convert wildcards for fields in command from
#       awk type to perl type.  Also, single quote wildcards
#       for better security.

# usage: &convert_command($Command);

sub convert_command {
  my $varname = shift;
  my $command = shift;
  my @new_cmd = ();

  $command =~ s/\$[0*]/\$$varname/g if $awk_field_syntax;

[...snip...]
}                                                             

ということで $0/$* 置き換えているようだ(シングルクォートするのが良いとある).

$ cat .swatchconf
watchfor        /test/
        exec echo $0; echo $*; echo $_
$ swatch --config-file=.swatchconf --tail-file=~/swatchtest --awk-field-syntax &
$ echo hogetest >> swatchtest
hogetest
hogetest
hogetest

コレでようやくマニュアルどおりの動きをする模様.man に書いといてくれよな.

ちなみに,swatch は設定ファイルを一時ファイルに書き出すのだが,そいつの場所は '--script-dir' オプションで指定する.ルートとかにできてしまうのはアホなので /var/run とか適当なところへ作るようにしておくべし.