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
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