arch/pkg/de-p1st-installer/lib/util.sh

84 lines
2.3 KiB
Bash
Raw Permalink Normal View History

2021-05-01 17:04:27 +02:00
function join_by() {
# Join array elements with character $1
#
# arg $1: delimiter
# arg $2: name of source array
# arg $3: variable name to store result
2021-05-01 17:04:27 +02:00
local -n ptr=$2 || return $?
local -n ptr2=$3 || return $?
ptr2=$(printf ',%s' "${ptr[@]}")
2021-05-01 17:04:27 +02:00
ptr2=${ptr2:1}
}
2021-05-01 12:25:25 +02:00
function newline_to_space() {
# Replaces all newlines with spaces
#
# arg $1: name of variable
2021-05-01 12:25:25 +02:00
2021-05-01 13:41:31 +02:00
local -n ptr=$1 || return $?
2021-05-01 12:25:25 +02:00
# Replace newlines with spaces
# 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.
# $1 and $2 can be equal (if the result shall be written to the input variable)
#
# arg $1: name of variable with newline separated list
# arg $2: name of array to store values into
2021-05-01 12:25:25 +02:00
2021-05-01 13:41:31 +02:00
local -n ptr=$1 || return $?
local -n ptr2=$2 || return $?
2021-05-01 12:25:25 +02:00
# ptr newline separated list.
# Store this as array ptr2.
readarray -t ptr2 <<<"${ptr}"
2021-05-01 12:25:25 +02:00
}
function space_separated_to_array() {
# arg $1: name of variable with space separated list
# arg $2: name of array to store values into
2021-05-01 12:25:25 +02:00
2021-05-01 13:41:31 +02:00
local -n ptr=$1 || return $?
2021-06-16 20:21:21 +02:00
# shellcheck disable=SC2178
2021-05-01 13:41:31 +02:00
local -n ptr2=$2 || return $?
2021-05-01 12:25:25 +02:00
# 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}")
2021-05-01 12:25:25 +02:00
}
2021-05-04 22:03:03 +02:00
function get_cpu_vendor() {
2021-05-13 20:12:57 +02:00
# @pre
# CPU_VENDOR ("", "autodetect")
2021-05-04 22:03:03 +02:00
# @post
2021-05-13 20:12:57 +02:00
# CPU_VENDOR ("amd", "intel", "none", "")
2021-05-04 22:03:03 +02:00
if [[ -z "${CPU_VENDOR}" ]] || [[ "${CPU_VENDOR}" == 'autodetect' ]]; then
2021-05-04 22:03:03 +02:00
2021-05-13 20:12:57 +02:00
# If run virtual environment (e.g. VirtualBox) then no CPU microcode is required
2021-05-13 20:23:32 +02:00
if cat /proc/cpuinfo | grep '^flags.*hypervisor' >/dev/null; then
echo 'Detected virtual environment.'
CPU_VENDOR='none'
2021-05-04 22:03:03 +02:00
else
2021-05-13 20:12:57 +02:00
local vendor_id
vendor_id=$(cat /proc/cpuinfo | grep vendor_id | head -1 | sed 's|vendor_id\s*:\s*||')
if [ "${vendor_id}" = 'AuthenticAMD' ]; then
CPU_VENDOR='amd'
elif [ "${vendor_id}" = 'GenuineIntel' ]; then
CPU_VENDOR='intel'
2021-05-13 20:12:57 +02:00
else
echo 'Unknown CPU vendor'
2021-05-13 20:12:57 +02:00
return 1
fi
2021-05-04 22:03:03 +02:00
fi
2021-05-13 20:12:57 +02:00
2021-05-04 22:03:03 +02:00
fi
}