From d7b3bfae8c0634bba9b03d0949a095bc4930175e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Thu, 15 Sep 2016 14:30:05 +0200 Subject: [PATCH] Add exit code for errors --- fullusb.sh | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/fullusb.sh b/fullusb.sh index b1afc1b..51bc4f6 100755 --- a/fullusb.sh +++ b/fullusb.sh @@ -14,7 +14,7 @@ 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}"; + printf "%40s\n" "${red}Error : Could not find \"$arg\" application.${normal}"; i=$((i+1)) fi done @@ -37,13 +37,20 @@ sourcemd5="/tmp/source_directory.md5" destmd5="/tmp/destination_directory.md5" case $keysearchid in - 0) printf "%40s\n" "${red}Alert: No usb key detected${normal}" ;; + 0) + printf "%40s\n" "${red}Alert : No usb key detected.${normal}" + exit 1 + ;; + 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 + printf "%40s\n" "${green}At least one key detected : Here we go${normal}" + # number of device (except sda) nbsd=$(ls /dev/sd* | grep -v sda | grep -v 1 | grep -v 2 | grep -v 3 | wc -l) + + # if $(lsusb) and $(ls /dev/sd*) doesn't give the same number if [[ $keysearchid != $nbsd ]]; then - printf "%40s\n" "${red}Alert: Error 1${normal} Keysearchid: ${keysearchid}" + printf "%40s\n" "${red}Alert : Error 1${normal} Keysearchid : ${keysearchid} and Numberofdevice : ${nbsd}" + exit 1 else # if everything is ok # Generate the md5sum for source directory pushd "${wherethefilesare}" @@ -51,8 +58,8 @@ case $keysearchid in popd 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 + # if $keysearchid=1 : for i in 1 + # if $keysearchid=2 : for i in 1 2 do [[ $i == 1 ]] && device="sdb" || device="sdc" # if 3 or more keys this line has to be adapt parted -s /dev/"${device}" mklabel msdos @@ -63,25 +70,34 @@ case $keysearchid in mount /dev/"${device}"1 "${mountpath}""${device}" cp -r $wherethefilesare/* "${mountpath}""${device}"/ sync + # Generate the md5sum for the mountpoint pushd "${mountpath}""${device}" find . -type f -exec md5sum {} \; | sort > "${destmd5}" popd + # Umount the key umount "${mountpath}""${device}" rmdir "${mountpath}""${device}" + # Compare the md5sum diff -q "${sourcemd5}" "${destmd5}" if [[ $? -gt 0 ]]; then - printf "%40s\n" "${red}Error: MD5 diff detected.${normal}"; - exit 1 + printf "%40s\n" "${red}Error : MD5 diff detected.${normal}"; + exit 2 else printf "%40s\n" "${green}MD5 check OK for ${device}.${normal}" fi + done + printf "%40s\n" "${green}DONE${normal}" fi ;; - *) printf "%40s\n" "${red}Alert: Error 2${normal}" ;; + + *) + printf "%40s\n" "${red}Alert : Error in the number of usb key detected.${normal}" + exit 3 + ;; esac exit 0