geerlingguy/macos-virtualbox-vm

Leverage Bash associative arrays

dotCipher opened this issue · 2 comments

It might be nice to consolidate the nested if ... else ... fi blocks into an iterative loop over a bash associative array. As new installers are supported they can be added in a declarative manner instead of a deeply nested if block.

Pseudocode would look something like:

# Init
declare -a installers
installers=(
    ["Sierra"]="Install macOS Sierra.app",
    ["ElCapitan"]="Install OS X El Capitan.app",
    ["Yosemite"]="Install OS X Yosemite.app"
)
# Loop
for iso_name in "${!installers[@]}"; do
    installerExists "${installers[$iso_name]}"
    result=$?
   if [ ${result} -eq 0 ] ; then
       createISO "${installers[$iso_name]}" "${iso_name}"
       exit 0
   fi
done
# Failure
echo "Could not find any installer"
exit 1

Associative arrays are only supported in Bash 4 (I think). I am running Sierra, and Sierra still has Bash 3.

Edit Yup! http://tldp.org/LDP/abs/html/bashver4.html

Closing; it would be nice to do this at some point, but the verbosity is still necessary for backwards compatibility :(

Someday :)