From fec7c872d9fc4accc41dcbf8c18877cf89a7cdb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Thu, 15 Sep 2016 09:42:10 +0200 Subject: [PATCH] Add a script to copy data to usb key --- fullusb.sh | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 fullusb.sh diff --git a/fullusb.sh b/fullusb.sh new file mode 100755 index 0000000..4e3e6ba --- /dev/null +++ b/fullusb.sh @@ -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 l’option -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 +