opus-data/usb-detect/usb-detect.sh
2021-03-11 16:45:24 +00:00

62 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
# udev script for mounting a block device
# and processing the files it contains
# To use it, place 10-usb-detect.rules
# in /etc/udev/rules.d
# Redirect stdout and stderr to log file
LOGFILE=/var/log/usb-detect.log
exec &>> $LOGFILE
# Processing function (file name is passed as parameter)
PROC_FUNC="Rscript /home/pi/git/opus-data/OpusData.R"
# Directory inside which the processing function is run
PROC_DIR="/home/pi/git/opus-data/"
echo "USB device detected at $DEVNAME"
MOUNTPOINT=/mnt$DEVNAME
echo "Mounting device at $MOUNTPOINT"
mkdir -p $MOUNTPOINT
mount $DEVNAME $MOUNTPOINT
echo "Extracting data..."
shopt -s nullglob
found=0
for f in $MOUNTPOINT/*.?
do
found=1
echo "Processing $f..."
(
cd $PROC_DIR
$PROC_FUNC $f
) || echo "Failed executing $PROC_FUNC $f in $PROC_DIR"
echo "Removing $f..."
rm $f
done
shopt -u nullglob
[ $found -eq 0 ] && echo "$MOUNTPOINT is empty"
echo "Umounting..."
umount $MOUNTPOINT
echo "Removing $MOUNTPOINT..."
rm -r $MOUNTPOINT
if [ -z `ls -A /mnt/dev` ]
then
echo "Removing /mnt/dev/..."
rmdir /mnt/dev
else
echo "Not removing /mnt/dev/ as other devices are mounted."
fi
echo "Done!"