1

私はもともと、PC に USB スティックを挿入したときに実行されるスクリプトと、それが取り外されたときに実行される別のスクリプトが必要でした。ドライブがマウントされたときにフォルダーを監視できる inotifywait を使用すると、探していた CREATE,ISDIR myfolder 出力が得られるため、これを使用して実際に外部スクリプトをトリガーすることは、私のプログラミング スキルを少し超えています。 EXPECT を見てきましたが、自分のタスクをどのように達成できるかわかりません。基本的には、以下に示すフローに従う期待スクリプトを作成する必要があると思います

Expect spawns the inotifywait process
expect then starts a loop
if the loop sees "CREATE,ISDIR test" then run script active.sh
if the loop sees "DELETE,ISDIR test" then run scrip inactive.sh
Loop

これを行うにはもっと簡単な方法があるかもしれませんが、私はいたるところを精査し、あらゆる種類のさまざまな組み合わせを試しました。一言で言えば、特定のフォルダーが作成されたときにスクリプトを実行し、削除されたときに別のスクリプトを実行したいのですが、これを行う簡単な方法は?

4

1 に答える 1

0

プロセスを生成して、必要な単語を待つだけです。それで全部です。

#!/usr/bin/expect
# Monitoring '/tmp/' directory
set watchRootDir "/tmp/"
# And, monitoring of folder named 'demo'
set watchFolder "demo"

puts "Monitoring root directory : '$watchRootDir'"
puts "Monitoring for folder : '$watchFolder'"

spawn  inotifywait -m -r -e create,delete /tmp
expect {
        timeout {puts "I'm waiting ...";exp_continue}
        "/tmp/ CREATE,ISDIR $watchFolder" {
            puts "Folder created"
            #  run active.sh here ...
            exp_continue
         }
        "/tmp/ DELETE,ISDIR $watchFolder" {
            puts "Folder deleted"
            #  run inactive.sh here ...
         }
}
# Sending 'Ctrl+C' to the program, so that it can quit 
# gracefully. 
send "\003"
expect eof

出力:

dinesh@myPc:~/stackoverflow$ ./Jason 
Monitoring root directory : '/tmp/'
Monitoring for folder : 'demo'
spawn inotifywait -m -r -e create,delete /tmp
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
I'm waiting ...
I'm waiting ...
/tmp/ CREATE,ISDIR demo
Folder created
I'm waiting ...
/tmp/ DELETE,ISDIR demo
Folder deleted

産卵中inotifywaitに、さらにいくつかのオプションを追加しました。-mデフォルトinotifywaitでは最初のイベントで終了し、-r再帰的に、またはサブディレクトリもチェックすることを意味するため、フラグは継続的な監視用です。

-e通知したいイベントのリストとともにフラグを指定する必要があります。そこで、ここでは、フォルダーのイベントを監視createします。delete

参考:inotifywait

于 2016-02-15T07:03:09.873 に答える