arch-installer/lib/util.sh

84 lines
2.3 KiB
Bash
Raw Normal View History

2022-07-16 17:18:03 +02:00
function join_by() {
# Join array elements with character $1
#
2023-03-16 17:30:32 +01:00
# arg $1: Delimiter
# arg $2: Name of source array
# arg $3: Variable name to store result
2022-07-16 17:18:03 +02:00
local -n ptr=$2 || return $?
local -n ptr2=$3 || return $?
ptr2=$(printf ',%s' "${ptr[@]}")
ptr2=${ptr2:1}
}
function newline_to_space() {
# Replaces all newlines with spaces
#
2023-03-16 17:30:32 +01:00
# arg $1: Name of variable
2022-07-16 17:18:03 +02:00
local -n ptr=$1 || return $?
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
# echo "replacing newlines in str: -->$ptr<--"
ptr="${ptr//$'\n'/' '}"
# echo "new str is: -->$ptr<--"
}
function newline_separated_to_array() {
# Watch out for tailing newlines as these will get an empty array entry at the end.
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
2022-07-16 17:18:03 +02:00
local -n ptr=$1 || return $?
local -n ptr2=$2 || return $?
# ptr newline separated list.
# Store this as array ptr2.
readarray -t ptr2 <<<"${ptr}"
}
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
2022-07-16 17:18:03 +02:00
local -n ptr=$1 || return $?
# shellcheck disable=SC2178
local -n ptr2=$2 || return $?
# ptr space separated list.
# Store this as array ptr2.
# Without newline at last array element: https://unix.stackexchange.com/a/519917/315162
readarray -d ' ' -t ptr2 < <(printf '%s' "${ptr}")
}
function get_cpu_vendor() {
# @pre
# CPU_VENDOR ("", "autodetect")
# @post
# CPU_VENDOR ("amd", "intel", "none", "")
if [[ -z "${CPU_VENDOR}" ]] || [[ "${CPU_VENDOR}" == 'autodetect' ]]; then
2023-03-16 17:30:32 +01:00
# If run virtual environment (e.g. VirtualBox) then no CPU microcode is required.
2023-03-16 17:20:48 +01:00
if grep '^flags.*hypervisor' /proc/cpuinfo >/dev/null; then
2022-07-16 17:18:03 +02:00
echo 'Detected virtual environment.'
CPU_VENDOR='none'
else
local vendor_id
2023-03-16 17:20:48 +01:00
vendor_id=$(grep vendor_id /proc/cpuinfo | head -1 | sed 's|vendor_id\s*:\s*||')
2022-07-16 17:18:03 +02:00
if [ "${vendor_id}" = 'AuthenticAMD' ]; then
CPU_VENDOR='amd'
elif [ "${vendor_id}" = 'GenuineIntel' ]; then
CPU_VENDOR='intel'
else
echo 'Unknown CPU vendor'
return 1
fi
fi
fi
}