29

ディレクトリを監視し、「EE」を含む行を削除してすべての新しいファイルを変更する inotify-tools を使用して bash スクリプトを作成しようとしています。変更すると、ファイルが別のディレクトリに移動されます

    #!/bin/sh
    while inotifywait -e create /home/inventory/initcsv; do
      sed '/^\"EE/d' Filein > fileout #how to capture File name?
      mv fileout /home/inventory/csvstorage
    fi
    done

助けてください?

4

3 に答える 3

25

デフォルトでは、からのテキスト出力inotifywait -e CREATE

     watched_filename CREATE event_filename

どこで をwatched_filename表し、新しいファイルの名前を表します/home/inventory/initcsvevent_filename

したがって、while inotifywait -e ...行の代わりに次のように入力します。

    DIR=/home/inventory/initcsv
    while RES=$(inotifywait -e create $DIR); do
        F=${RES#?*CREATE }

あなたのsed$Fでは名前として使用しますFilein$(...)構成はプロセス置換の posix 互換形式 (多くの場合、バックティックを使用して行われます) であり、${RES#pattern}結果は$RES最短のパターン一致プレフィックスを削除したものと同じであることに注意してください。パターンの最後の文字は空白であることに注意してください。 [アップデート 2 を参照]

更新 1空白を含む可能性のあるファイル名を処理するには、sed 行"$F"$F. つまり、 の値への参照を二重引用符で囲みますF

RES=...とのF=...定義では二重引用符を使用する必要はありませんが、必要に応じて使用してもかまいません。たとえば 、空白を含むファイル名を処理する場合F=${RES#?*CREATE }F="${RES#?*CREATE }"両方とも問題なく機能します。

更新 2 Daan のコメントに記載されinotifywaitているように、出力の形式を制御する--formatパラメーターがあります。コマンド付き

while RES=$(inotifywait -e create $DIR --format %f .)
   do echo RES is $RES at `date`; done

1 つの端末とコマンドで実行

touch a aa; sleep 1; touch aaa;sleep 1; touch aaaa

別の端末で実行すると、次の出力が最初の端末に表示されました。

Setting up watches.
Watches established.
RES is a at Tue Dec 31 11:37:20 MST 2013
Setting up watches.
Watches established.
RES is aaa at Tue Dec 31 11:37:21 MST 2013
Setting up watches.
Watches established.
RES is aaaa at Tue Dec 31 11:37:22 MST 2013
Setting up watches.
Watches established.
于 2011-09-24T23:25:04.180 に答える
13

からの出力inotifywaitは次の形式です。

filename eventlist [eventfilename]

ファイル名にスペースとコンマを含めることができる場合、これは解析が難しくなります。'sane'ファイル名のみが含まれている場合は、次の操作を実行できます。

srcdir=/home/inventory/initcsv
tgtdir=/home/inventory/csvstorage
inotifywait -m -e create "$directory" |
while read filename eventlist eventfile
do
    sed '/^"EE/d'/' "$srcdir/$eventfile" > "$tgtdir/$eventfile" &&
    rm -f "$srcdir/$eventfile
done
于 2011-09-24T23:35:57.810 に答える
1

inotifywait の man ページを引用します。

inotifywait will output diagnostic information on standard error and event information  on
   standard  output.  The event output can be configured, but by default it consists of lines
   of the following form:

   watched_filename EVENT_NAMES event_filename

   watched_filename
          is the name of the file on which the event occurred.  If the file is a directory, a
          trailing slash is output.

つまり、ファイルの名前を標準出力に出力します。したがって、標準出力からそれらを読み取り、それらを操作して、やりたいことを行う必要があります。

于 2011-09-24T23:25:21.493 に答える