Add a script to copy data to usb key

This commit is contained in:
Jeremy Gardais 2016-09-15 09:42:10 +02:00
parent 529a7a02fb
commit fec7c872d9
1 changed files with 63 additions and 0 deletions

63
fullusb.sh Executable file
View File

@ -0,0 +1,63 @@
#!/bin/bash
# script to copy data on usb keys
# dependances : aptitude install ncurses-bin parted dosfstools (mlabel devenu inutile)
# text color in bash
normal=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 2)
# dependencies check
i=0
for arg in "tput" "parted" "mkdosfs"; do
hash "${arg}"
if [[ $? -gt 0 ]]; then
printf "%40s\n" "${red}Error: Could not find \"$arg\" application.${normal}";
i=$((i+1))
fi
done
[[ $i -eq 0 ]] || exit $i
# this part depends of the event
keylabel="OPTIQUE2015" # label of the keys
wherethefilesare="/homes/carre/optique2015" # where are the files you need to copy
badkeyid="abcd" # depends of key models (lsusb)
goodkeyid="058f"
# all our usb keys have the same ID, we decided to write only on these keys
keysearchbadid=$(lsusb | grep $badkeyid | wc -l)
keysearchgoodid=$(lsusb | grep $goodkeyid | wc -l)
keysearchid=$(($keysearchbadid+$keysearchgoodid))
case $keysearchid in
0) printf "%40s\n" "${red}Alert: No usb key detected${normal}" ;;
1|2) # we only have 1 or 2 usb ports
printf "%40s\n" "${green}At least one key detected: Here we go${normal}"
# si lsusb et ls /dev/sd* ne donnent pas le même nombre de clés → erreur
nbsd=$(ls /dev/sd* | grep -v sda | grep -v 1 | grep -v 2 | grep -v 3 | wc -l)
if [[ $keysearchid != $nbsd ]]; then
printf "%40s\n" "${red}Alert: Error 1${normal} Keysearchid: ${keysearchid}"
else # if everything is ok
for i in $(seq 1 $keysearchid) # from 1 to $keysearchid by step of 1
# if $keysearchid=1: for i in 1
# if $keysearchid=2: for i in 1 2
do
[[ $i == 1 ]] && k="sdb" || k="sdc" # if 3 or more keys this line has to be adapt
parted -s /dev/$k mklabel msdos
parted /dev/$k mkpart -a optimal primary fat32 0% 100%
mkdosfs -F 32 -I -n $keylabel /dev/${k}1
# fatlabel /dev/${k}1 $keylabel # Cette commande est remplacée par loption -n de la commande précédente
mkdir /mnt/usbkey$k
mount /dev/${k}1 /mnt/usbkey$k
cp -r $wherethefilesare/* /mnt/usbkey$k/
sync
umount /mnt/usbkey$k
rmdir /mnt/usbkey$k
done
printf "%40s\n" "${green}DONE${normal}"
fi ;;
*) printf "%40s\n" "${red}Alert: Error 2${normal}" ;;
esac