Skip to content

Instantly share code, notes, and snippets.

@tong
Last active February 11, 2016 12:04
Show Gist options
  • Save tong/6278125 to your computer and use it in GitHub Desktop.
Save tong/6278125 to your computer and use it in GitHub Desktop.
Autoexecute scripts on external drives on mount
#!/bin/bash
HOST=$(cat /etc/hostname)
SCRIPTFILE="autoexe_$HOST.sh"
MOUNTDELAY=3
if [ -z "$1" ]; then
MOUNTDIR=/media
else
MOUNTDIR=$1
fi
echo "Watching for disks $MOUNTDIR"
inotifywait -me create --format %w%f $MOUNTDIR | while read DISKPATH
do
SCRIPT="$DISKPATH/$SCRIPTFILE"
sleep $MOUNTDELAY
if [ $? -eq 0 ]; then
zenity --display=:0.0 --question --text="Execute $SCRIPT ?"
if [ $? -eq 0 ]; then
RESULT=$(/bin/bash "$SCRIPT")
RESULT="$RESULT\n\n---\nEject drive?"
zenity --display=:0.0 --question --title="$SCRIPT Result and Question" --text="$RESULT"
if [ $? -eq 0 ]; then
umount "$DISKPATH"
fi
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment