2016-02-12 25 views
1

文件夹中的变化我本来想拥有,当我在我的电脑插入一个USB记忆棒跑一个脚本,然后另一个脚本,当它被拆除,我使用udev搞砸周围没有任何的成功使这显然不是最好的选择,我然后在inotifywait,我可以去观看时,我的驱动器是安装在一个文件夹来了,因为这将创建,ISDIR MyFolder文件输出我一直在寻找,但使用给我这实际上触发外部脚本是有点超出我的编程技能,我已经看过的处置,但不能看我怎么会实现我的任务,我想基本上我需要创建如下图所示使用期望和inotifywait看在Linux上

下面的流脚本的期待
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 

可能有一个更简单的方法来做到这一点,但我已到处搜索,并尝试各种不同的组合,简而言之,我希望脚本在创建某个文件夹时运行,然后在删除时运行另一个脚本,在那里一个简单的方法来做到这一点?

回答

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 

输出:

[email protected]:~/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标志。所以,在这里,我们要监视文件夹的createdelete事件。

参考:inotifywait