2022-07-16 17:18:03 +02:00
|
|
|
function join_by() {
|
|
|
|
# Join array elements with character $1
|
|
|
|
#
|
2023-03-21 14:11:10 +01:00
|
|
|
# arg $1: A single-character delimiter, e.g. ','
|
2023-03-16 17:30:32 +01:00
|
|
|
# arg $2: Name of source array
|
|
|
|
# arg $3: Variable name to store result
|
2023-03-21 14:11:10 +01:00
|
|
|
validate_args 3 "$@" || return $?
|
|
|
|
local delimiter="${1}"
|
|
|
|
local -n source_array_ptr=$2
|
|
|
|
local -n joined_ptr=$3
|
2022-07-16 17:18:03 +02:00
|
|
|
|
2023-03-21 14:11:10 +01:00
|
|
|
# https://stackoverflow.com/a/2317171
|
|
|
|
joined_ptr=$(printf "${delimiter}%s" "${source_array_ptr[@]}") || return $?
|
|
|
|
joined_ptr=${joined_ptr:1} || return $?
|
2022-07-16 17:18:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function newline_to_space() {
|
|
|
|
# Replaces all newlines with spaces
|
|
|
|
#
|
2023-03-16 17:30:32 +01:00
|
|
|
# arg $1: Name of variable
|
2023-03-21 14:11:10 +01:00
|
|
|
validate_args 1 "$@" || return $?
|
|
|
|
local -n ptr=$1
|
2022-07-16 17:18:03 +02:00
|
|
|
|
2023-03-16 17:30:32 +01:00
|
|
|
# Replace newlines with spaces.
|
2022-07-16 17:18:03 +02:00
|
|
|
# See bash string substitution: https://gist.github.com/JPvRiel/b279524d3e56229a896477cb8082a72b
|
|
|
|
ptr="${ptr//$'\n'/' '}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function newline_separated_to_array() {
|
2023-03-21 14:11:10 +01:00
|
|
|
# Watch out for tailing newlines as these result in additional elements "" at the end of the array.
|
2023-03-16 17:30:32 +01:00
|
|
|
# $1 and $2 can be equal (if the result shall be written to the input variable).
|
2022-07-16 17:18:03 +02:00
|
|
|
#
|
2023-03-16 17:30:32 +01:00
|
|
|
# arg $1: Name of variable with newline separated list
|
|
|
|
# arg $2: Name of array to store values into
|
2023-03-21 14:11:10 +01:00
|
|
|
validate_args 2 "$@" || return $?
|
|
|
|
local -n source_ptr=$1
|
|
|
|
local -n array1_ptr=$2
|
2022-07-16 17:18:03 +02:00
|
|
|
|
2023-03-21 14:11:10 +01:00
|
|
|
# source_ptr is a newline separated list.
|
|
|
|
# Store this as array array1_ptr.
|
|
|
|
# shellcheck disable=SC2034
|
|
|
|
readarray -t array1_ptr <<<"${source_ptr}"
|
2022-07-16 17:18:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function space_separated_to_array() {
|
2023-03-16 17:30:32 +01:00
|
|
|
# arg $1: Name of variable with space separated list
|
|
|
|
# arg $2: Name of array to store values into
|
2023-03-21 14:11:10 +01:00
|
|
|
validate_args 2 "$@" || return $?
|
|
|
|
local -n source_ptr=$1
|
2022-07-16 17:18:03 +02:00
|
|
|
# shellcheck disable=SC2178
|
2023-03-21 14:11:10 +01:00
|
|
|
local -n array2_ptr=$2
|
|
|
|
|
|
|
|
# source_ptr space separated list.
|
|
|
|
# Store this as array array2_ptr.
|
2022-07-16 17:18:03 +02:00
|
|
|
# Without newline at last array element: https://unix.stackexchange.com/a/519917/315162
|
2023-03-21 14:11:10 +01:00
|
|
|
# shellcheck disable=SC2034
|
|
|
|
readarray -d ' ' -t array2_ptr < <(printf '%s' "${source_ptr}")
|
2022-07-16 17:18:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_cpu_vendor() {
|
2023-03-21 14:11:10 +01:00
|
|
|
# If $1 does already contain a known CPU vendor, nothing is done.
|
|
|
|
#
|
|
|
|
# Otherwise, we try to detect the CPU vendor and save it into $1.
|
|
|
|
#
|
|
|
|
# arg $1: Name of variable to store CPU vendor into.
|
|
|
|
#
|
2022-07-16 17:18:03 +02:00
|
|
|
# @pre
|
2023-03-21 14:11:10 +01:00
|
|
|
# $1 ("amd", "intel", "none", *)
|
2022-07-16 17:18:03 +02:00
|
|
|
# @post
|
2023-03-21 14:11:10 +01:00
|
|
|
# $1 ("amd", "intel", "none")
|
|
|
|
validate_args 1 "$@" || return $?
|
|
|
|
local -n cpu_vendor_ptr=$1
|
2022-07-16 17:18:03 +02:00
|
|
|
|
2023-03-21 14:11:10 +01:00
|
|
|
# Check if variable does already contain known CPU vendor.
|
|
|
|
case "${cpu_vendor_ptr}" in
|
|
|
|
amd|intel|none)
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
:
|
|
|
|
;;
|
|
|
|
esac
|
2022-07-16 17:18:03 +02:00
|
|
|
|
2023-03-21 14:11:10 +01:00
|
|
|
# If in virtual environment (e.g. VirtualBox), then no CPU microcode is required.
|
|
|
|
if grep --quiet '^flags.*hypervisor' /proc/cpuinfo; then
|
|
|
|
echo 'Detected virtual environment.'
|
|
|
|
cpu_vendor_ptr='none'
|
|
|
|
return 0
|
|
|
|
fi
|
2022-07-16 17:18:03 +02:00
|
|
|
|
2023-03-21 14:11:10 +01:00
|
|
|
local vendor_id
|
|
|
|
vendor_id="$(grep vendor_id /proc/cpuinfo | head -1 | sed 's|vendor_id\s*:\s*||')"
|
2022-07-16 17:18:03 +02:00
|
|
|
|
2023-03-21 14:11:10 +01:00
|
|
|
if [ "${vendor_id}" = 'AuthenticAMD' ]; then
|
|
|
|
cpu_vendor_ptr='amd'
|
|
|
|
return 0
|
|
|
|
elif [ "${vendor_id}" = 'GenuineIntel' ]; then
|
|
|
|
cpu_vendor_ptr='intel'
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
echo 'Unknown CPU vendor'
|
|
|
|
return 1
|
2022-07-16 17:18:03 +02:00
|
|
|
fi
|
|
|
|
}
|