Ich habe einige Ruby-Skripte verbessert, die ich gefunden habe, um genau das zu tun, wonach Sie suchen. Hier ist der Ruby-Code:
#!/usr/bin/ruby # Inspired by http://vikhyat.net/code/snippets/#watchfile # How to use: # This script takes two paramaters: a folder and a shell command # The script watches for when files in the folder are changed. When they are, the shell command executes # Here are some shortcuts you can put in the shell script: # %F = filename (with extension) # %B = base filename (without extension) unless ARGV.length == 2 puts "\e[32m Usage: \e[0mruby OnSaveFolder.rb 'folder' 'command'" exit end require 'digest/md5' require 'ftools' $md5 = '' $directory = File.expand_path(ARGV[0]) $contents = `ls #{$directory}` $contentsList = $contents.split("\n") $fileList = [] Dir.chdir($directory) for i in $contentsList if ( File.file?(File.expand_path(i)) == true) $fileList.push(i) end end $processes = [] def watch(file, timeout, &cb) $md5 = Digest::MD5.hexdigest(File.read(file)) loop do sleep timeout if ( temp = Digest::MD5.hexdigest(File.read(file)) ) != $md5 $md5 = temp cb.call(file) end end end puts "\e[31m Watching files in folder \e[34m #{$directory}" puts "\e[32m Press Control+C to stop \e[0m" for i in $fileList pid = fork do $num = 0 $filePath = File.expand_path(i) watch i, 1 do |file| puts "\n # saved" $command = ARGV[1].dup if ( ARGV[1].include?('%F') ) $command = $command.gsub!('%F', i) end if ( ARGV[1].include?('%B') ) $command = $command.gsub!('%B', File.basename(i, '.*')) end $output = `#{$command}` if ( $output != '') puts "\e[34m #{$command}\e[31m output: \e[0m" puts $output end puts "\e[34m #{$command}\e[31m finished \e[0m (#{$num}, #)\n" $num += 1 end end $processes.push(pid) end Process.wait(pid) for i in $processes `kill #` end
Ich habe dieses Skript "OnSaveFolder.rb" genannt. Es sind zwei Parameter erforderlich: der Ordner, den Sie auf Änderungen überwachen möchten, und den Bash-Code, den Sie bei einer Änderung ausführen möchten. Zum Beispiel,
ruby OnSaveFolder.rb '/home/Movies' 'echo "A movie has been changed!"'
Ich hoffe das hilft! Ich habe festgestellt, dass Ruby für diese Art von Dingen sehr gut funktioniert und standardmäßig unter OS X installiert ist.