From 38bf1de80ce30575957a581a74af715e28056ef3 Mon Sep 17 00:00:00 2001 From: oddlama Date: Sat, 14 May 2022 00:48:42 +0200 Subject: [PATCH] chore: improve wanted program check (remove array indirection) --- scripts/functions.sh | 2 +- scripts/utils.sh | 44 +++++++++++++++++++++++--------------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/scripts/functions.sh b/scripts/functions.sh index 5d4c065..0371680 100644 --- a/scripts/functions.sh +++ b/scripts/functions.sh @@ -73,7 +73,7 @@ function prepare_installation_environment() { ntpd partprobe python3 - ?rhash + "?rhash" sha512sum sgdisk uuidgen diff --git a/scripts/utils.sh b/scripts/utils.sh index b3ddca9..f6456dc 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -351,11 +351,22 @@ function parse_arguments() { fi } -# $1: array -# $@: elements -function append_to_array() { - declare -n _target_array=$1; shift - _target_array+=("$@") +# $1: program +# $2: checkfile +function has_program() { + local program="$1" + local checkfile="$2" + if [[ -z "$checkfile" ]]; then + type "$program" &>/dev/null \ + || return 1 + elif [[ "${checkfile:0:1}" == "/" ]]; then + [[ -e "$checkfile" ]] \ + || return 1 + else + type "$checkfile" &>/dev/null \ + || return 1 + fi + return 0 } function check_wanted_programs() { @@ -367,25 +378,16 @@ function check_wanted_programs() { for tuple in "$@"; do program="${tuple%%=*}" checkfile="${tuple##*=}" - if [[ "$program" == "?"* ]]; then - program="${program#"?"}" - arr=missing_wanted - else - arr=missing_required - fi - if [[ -z "$checkfile" ]]; then - type "$program" &>/dev/null \ - || append_to_array "$arr" "$program" - elif [[ "${checkfile:0:1}" == "/" ]]; then - [[ -e "$checkfile" ]] \ - || append_to_array "$arr" "$program" - else - type "$checkfile" &>/dev/null \ - || append_to_array "$arr" "$program" + if ! has_program "${program#"?"}" "$checkfile"; then + if [[ "$program" == "?"* ]]; then + missing_wanted+=("${program#"?"}") + else + missing_required+=("$program") + fi fi done - [[ "${#missing_required[@]}" -eq 0 && "${#missing_required[@]}" -eq 0 ]] \ + [[ "${#missing_required[@]}" -eq 0 && "${#missing_wanted[@]}" -eq 0 ]] \ && return if [[ "${#missing_required[@]}" -gt 0 ]]; then