Last active
August 29, 2015 14:03
-
-
Save mmb/a63781752286f431d079 to your computer and use it in GitHub Desktop.
When USB drives are mounted, check them for a script called plz_verify.sh and run it if found.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# When USB drives are mounted, check them for a script called plz_verify.sh | |
# and run it if found. | |
# | |
# symlink all USB volumes you want to check into ~/usb | |
# ln -s /Volumes/mmb ~/usb/mmb | |
require 'logger' | |
require 'set' | |
logger = Logger.new(STDOUT) | |
state = Set.new | |
while true do | |
# the extra stars are for symlinks | |
current = Set.new(Dir.glob("#{Dir.home}/usb/**/*/**/plz_verify.sh")) | |
gone = state - current | |
new = current - state | |
state = current | |
gone.each do |script| | |
logger.info("#{script} unmounted") | |
end | |
new.each do |script| | |
logger.info("running #{script}") | |
script_contents = File.read(script) | |
IO.popen('sh', 'w') do |io| | |
io.write(script_contents) | |
end | |
end | |
sleep 2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment