Running fsck with other program and UDEV in bash

558 Views Asked by At

After much trouble I got the UDEV rule to run after inserting an USB. It runs a program to convert the names of pictures and movies. I use {} & to run the program in the background: The only thing is that by unplugging the usb it is easy corrupted. So I would like to also run fsck. Does anybody has an idea?

Here is the UDEV rule:

CTION=="add", SUBSYSTEM=="block", ATTRS{idVendor}=="14cd", ATTRS{idProduct}=="121f", RUN+="/home/pi/bashtest.sh"

Here is the program:

#!/bin/bash
sudo umount /dev/sda1
sudo fsck -y /dev/sda1
{
dd=1234567890aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ
sleep 5
sudo mount -t vfat /dev/sda1 /media/usb1
cd /media/usb1/DCIM/Camera
sudo find /media/usb1/DCIM/Camera -regextype posix-egrep -regex ".*[^/]{13}.JPG"|
for i in *.JPG
do
ddate=$(exiv2 "${i}"|grep timestamp)
SPEC=$ddate
read X X YEAR MONTH DAY HOUR MINUTE SECOND <<<${SPEC//:/ }
d1=${YEAR:2}
d2=${dd:(10#$MONTH-1):1}
d3=${dd:(10#$DAY-1):1}
d4=${dd:(10#$HOUR-1):1}
d5=${dd:(10#$MINUTE-1):1}
d6=${dd:(10#$SECOND-1):1}
d7=0
sudo cp -nrv --preserve=all "$i" /media/usb1/DCIM/"${d1}${d2}${d3}${d4}${d5}${d6}${d7}.JPG"
find . -name '*.JPG' -size -1 -delete
done
for i in *.MP4
do
#exiftool -createdate -S -s 20140308_133017.MP4
dddate=$(exiftool "${i}" |grep "Media Create Date" | awk -F':' '{print $2, $3, $4, $5, $6, $7}')
SPEC=$dddate
read YEAR MONTH DAY HOUR MINUTE SECOND <<<${SPEC//:/ }
d1=${YEAR:2}
d2=${dd:(10#$MONTH-1):1}
d3=${dd:(10#$DAY-1):1}
d4=${dd:(10#$HOUR-1):1}
d5=${dd:(10#$MINUTE-1):1}
d6=${dd:(10#$SECOND-1):1}
d7=0
sudo cp -nrv --preserve=all "$i" /media/usb1/DCIM/"${d1}${d2}${d3}${d4}${d5}${d6}${d7}.MP4"
done
sudo umount -l /media/usb1
sleep 5
sudo shutdown -h now
} &

Probably the code can be written better, but it works for me.

1

There are 1 best solutions below

0
On

Using {} && for fsck and the rest {} & let it all work on the background and helped me to get it to work!