Skip to content

Commit

Permalink
Rework array length checks
Browse files Browse the repository at this point in the history
Using the 'set -u' flag triggers 'unbound variable' errors
in cases where the array length may be empty. I have reworked
the checks to use what appears to be an arcane construct for
handling empty arrays properly.

refs WhyAskWhy#9
  • Loading branch information
deoren committed Jan 28, 2018
1 parent 5aa011a commit db0cf4d
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions email_updates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set -e
# * http://mywiki.wooledge.org/BashPitfalls
# * http://stackoverflow.com/questions/1063347/passing-arrays-as-parameters-in-bash
# * http://stackoverflow.com/questions/7442417/how-to-sort-an-array-in-bash
# * https://serverfault.com/questions/477503/check-if-array-is-empty-in-bash

#########################
# Settings
Expand Down Expand Up @@ -558,7 +559,7 @@ AVAILABLE_UPDATES=($(calculate_updates_available))


# If updates are available ...
if [[ ${#AVAILABLE_UPDATES[@]} -gt 0 ]]; then
if [[ ${AVAILABLE_UPDATES[@]:+${AVAILABLE_UPDATES[@]}} ]]; then

declare -a UNREPORTED_UPDATES SKIPPED_UPDATES

Expand All @@ -568,7 +569,7 @@ if [[ ${#AVAILABLE_UPDATES[@]} -gt 0 ]]; then
if $(is_patch_already_reported ${update}); then

# Skip the update, but log it for troubleshooting purposes
SKIPPED_UPDATES=("${SKIPPED_UPDATES[@]}" "${update}")
SKIPPED_UPDATES+=("${update}")

if [[ "${VERBOSE_DEBUG_ON}" -ne 0 ]]; then
echo "[SKIP] ${update}"
Expand All @@ -577,7 +578,8 @@ if [[ ${#AVAILABLE_UPDATES[@]} -gt 0 ]]; then
else
# Add the update to an array to be reported
# FIXME: There is a bug here that results in a duplicate item
UNREPORTED_UPDATES=("${UNREPORTED_UPDATES[@]}" "${update}")
# TODO: Confirm if this is still an issue
UNREPORTED_UPDATES+=("${update}")

if [[ "${VERBOSE_DEBUG_ON}" -ne 0 ]]; then
echo "[INCL] ${update}"
Expand All @@ -591,18 +593,19 @@ if [[ ${#AVAILABLE_UPDATES[@]} -gt 0 ]]; then
print_patch_arrays
fi

# If we're not in debug mode, send an email
if [[ "${DEBUG_ON}" -eq 0 ]]; then
# If there are no updates, DON'T send an email
if [[ ! ${#UNREPORTED_UPDATES[@]} -gt 0 ]]; then
:
else
# There ARE updates, so send the email
# If there are updates that not have been reported already ...
if [[ ${UNREPORTED_UPDATES[@]:+${UNREPORTED_UPDATES[@]}} ]]; then

# If we're not in debug mode, send an email
if [[ "${DEBUG_ON}" -eq 0 ]]; then

email_report "${UNREPORTED_UPDATES[@]}"
fi
fi

record_reported_patches "${UNREPORTED_UPDATES[@]}"
# Record the updates, regardless of whether an email was sent.
record_reported_patches "${UNREPORTED_UPDATES[@]}"

fi

else

Expand Down

0 comments on commit db0cf4d

Please sign in to comment.