mirror of
https://github.com/vincentmli/bpfire.git
synced 2026-04-09 18:45:54 +02:00
Test if a given array has the specified value stored under key. ! does not work here to access the array by variable name. So the solution here is: https://unix.stackexchange.com/questions/60584/how-to-use-a-variable-as-part-of-an-array-name/60585#60585 Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org> Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
42 lines
1.0 KiB
Bash
42 lines
1.0 KiB
Bash
#!/usr/bin/bash
|
|
|
|
# Get the path of this file.
|
|
# This ist rather complex as we do not want the calling script file
|
|
# That why we use BASH_SOURCE[0]
|
|
LIB_DIR="$(readlink -f "${BASH_SOURCE[0]}")"
|
|
# In LIB_DIR is currently saved the path to this file you are currently reading
|
|
# but we need the directory where it is located so:
|
|
LIB_DIR="$(dirname "${LIB_DIR}")"
|
|
|
|
|
|
. ${LIB_DIR}/lib_color.sh
|
|
|
|
test_command() {
|
|
|
|
if ! "$@" ; then
|
|
echo -e "${CLR_RED_BG} Test failed: ${*} ${CLR_RESET}"
|
|
return 1
|
|
else
|
|
echo -e "${CLR_GREEN_BG} Test succeded: ${*} ${CLR_RESET}"
|
|
return 0
|
|
fi
|
|
}
|
|
|
|
var_has_value() {
|
|
[[ "${!1}" == "${2}" ]]
|
|
}
|
|
|
|
test_value_in_array() {
|
|
local -n array="${1}"
|
|
local key="${2}"
|
|
local value="${3}"
|
|
|
|
if [[ "${array[${key}]}" == "${value}" ]] ; then
|
|
echo -e "${CLR_GREEN_BG}Test succeded: The array '${1}' contains the value '${value}' under the key '${key}' ${CLR_RESET}"
|
|
return 0
|
|
else
|
|
echo -e "${CLR_RED_BG}Test failed: The array '${1}' contains the value '${array[${key}]}' under the key '${key} and not '${value}' ${CLR_RESET}"
|
|
return 1
|
|
fi
|
|
}
|