Add exit code for errors

This commit is contained in:
Jeremy Gardais 2016-09-15 14:30:05 +02:00
parent caa057fdb8
commit d7b3bfae8c
1 changed files with 26 additions and 10 deletions

View File

@ -14,7 +14,7 @@ i=0
for arg in "tput" "parted" "mkdosfs"; do for arg in "tput" "parted" "mkdosfs"; do
hash "${arg}" hash "${arg}"
if [[ $? -gt 0 ]]; then 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)) i=$((i+1))
fi fi
done done
@ -37,13 +37,20 @@ sourcemd5="/tmp/source_directory.md5"
destmd5="/tmp/destination_directory.md5" destmd5="/tmp/destination_directory.md5"
case $keysearchid in 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 1|2) # we only have 1 or 2 usb ports
printf "%40s\n" "${green}At least one key detected: Here we go${normal}" 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 # number of device (except sda)
nbsd=$(ls /dev/sd* | grep -v sda | grep -v 1 | grep -v 2 | grep -v 3 | wc -l) 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 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 else # if everything is ok
# Generate the md5sum for source directory # Generate the md5sum for source directory
pushd "${wherethefilesare}" pushd "${wherethefilesare}"
@ -51,8 +58,8 @@ case $keysearchid in
popd popd
for i in $(seq 1 $keysearchid) # from 1 to $keysearchid by step of 1 for i in $(seq 1 $keysearchid) # from 1 to $keysearchid by step of 1
# if $keysearchid=1: for i in 1 # if $keysearchid=1: for i in 1
# if $keysearchid=2: for i in 1 2 # if $keysearchid=2: for i in 1 2
do do
[[ $i == 1 ]] && device="sdb" || device="sdc" # if 3 or more keys this line has to be adapt [[ $i == 1 ]] && device="sdb" || device="sdc" # if 3 or more keys this line has to be adapt
parted -s /dev/"${device}" mklabel msdos parted -s /dev/"${device}" mklabel msdos
@ -63,25 +70,34 @@ case $keysearchid in
mount /dev/"${device}"1 "${mountpath}""${device}" mount /dev/"${device}"1 "${mountpath}""${device}"
cp -r $wherethefilesare/* "${mountpath}""${device}"/ cp -r $wherethefilesare/* "${mountpath}""${device}"/
sync sync
# Generate the md5sum for the mountpoint # Generate the md5sum for the mountpoint
pushd "${mountpath}""${device}" pushd "${mountpath}""${device}"
find . -type f -exec md5sum {} \; | sort > "${destmd5}" find . -type f -exec md5sum {} \; | sort > "${destmd5}"
popd popd
# Umount the key # Umount the key
umount "${mountpath}""${device}" umount "${mountpath}""${device}"
rmdir "${mountpath}""${device}" rmdir "${mountpath}""${device}"
# Compare the md5sum # Compare the md5sum
diff -q "${sourcemd5}" "${destmd5}" diff -q "${sourcemd5}" "${destmd5}"
if [[ $? -gt 0 ]]; then if [[ $? -gt 0 ]]; then
printf "%40s\n" "${red}Error: MD5 diff detected.${normal}"; printf "%40s\n" "${red}Error: MD5 diff detected.${normal}";
exit 1 exit 2
else else
printf "%40s\n" "${green}MD5 check OK for ${device}.${normal}" printf "%40s\n" "${green}MD5 check OK for ${device}.${normal}"
fi fi
done done
printf "%40s\n" "${green}DONE${normal}" printf "%40s\n" "${green}DONE${normal}"
fi ;; 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 esac
exit 0 exit 0