Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ading2210
GitHub Repository: ading2210/shimboot
Path: blob/main/common.sh
402 views
1
#!/bin/bash
2
3
set -e
4
if [ "$DEBUG" ]; then
5
set -x
6
export DEBUG=1
7
fi
8
9
ANSI_CLEAR='\033[0m'
10
ANSI_BOLD='\033[1m'
11
ANSI_RED='\033[1;31m'
12
ANSI_GREEN='\033[1;32m'
13
ANSI_BLUE='\033[1;34m'
14
15
check_deps() {
16
local needed_commands="$1"
17
for command in $needed_commands; do
18
if ! command -v $command &> /dev/null; then
19
echo " - $command"
20
fi
21
done
22
}
23
24
assert_deps() {
25
local needed_commands="$1"
26
local missing_commands=$(check_deps "$needed_commands")
27
if [ "${missing_commands}" ]; then
28
print_error "You are missing dependencies needed for this script."
29
print_error "Commands needed:"
30
print_error "${missing_commands}"
31
exit 1
32
fi
33
}
34
35
parse_args() {
36
declare -g -A args
37
for argument in "$@"; do
38
if [ "$argument" = "-h" ] || [ "$argument" = "--help" ]; then
39
print_help
40
exit 0
41
fi
42
43
local key=$(echo $argument | cut -f1 -d=)
44
local key_length=${#key}
45
local value="${argument:$key_length+1}"
46
args["$key"]="$value"
47
done
48
}
49
50
assert_root() {
51
if [ "$EUID" -ne 0 ]; then
52
print_error "This script needs to be run as root."
53
exit 1
54
fi
55
}
56
57
assert_args() {
58
if [ -z "$1" ]; then
59
print_help
60
exit 1
61
fi
62
}
63
64
print_title() {
65
printf ">> ${ANSI_GREEN}${1}${ANSI_CLEAR}\n"
66
}
67
68
print_info() {
69
printf "${ANSI_BOLD}${1}${ANSI_CLEAR}\n"
70
}
71
72
print_error() {
73
printf "${ANSI_RED}${1}${ANSI_CLEAR}\n"
74
}
75