-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.sh
58 lines (48 loc) · 1.29 KB
/
lib.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function detect_distro()
{
local root_path="$1"
local str_check="$2"
local distro_id='none'
local etc_path
declare -a debian_family=('debian' 'ubuntu' 'raspbian')
declare -a arch_family=('arch' 'manjaro')
declare -a fedora_family=('fedora')
etc_path=$(join_path "$root_path" /etc)
if [[ -n "$str_check" ]]; then
distro_id="$str_check"
elif [[ -d $etc_path ]]; then
distro_id=$(cat "$etc_path"/*-release | grep -w ID | cut -d = -f 2)
fi
# Debian family
if [[ "${debian_family[*]}" =~ ${distro_id} ]]; then
printf '%s\n' 'debian'
# ArchLinux family
elif [[ "${arch_family[*]}" =~ ${distro_id} ]]; then
printf '%s\n' 'arch'
# Fedora family
elif [[ "${fedora_family[*]}" =~ ${distro_id} ]]; then
printf '%s\n' 'fedora'
else
printf '%s\n' 'none'
fi
}
function join_path()
{
local target_path=$1
local member=$2
local joined
# TODO: Extended pattern matching. We should consider to use it as a default
# in this project.
shopt -s extglob
member=${member%%+(/)}
member=${member##+(/)}
joined="${target_path%%+(/)}/$member"
printf '%s\n' "$joined" | tr -s '/'
}
function install_packages()
{
local -n list="$1"
local package_manager_cmd="$2"
printf -v list_str '%s ' "${list[@]}"
eval "${package_manager_cmd} ${list_str}"
}