-
Notifications
You must be signed in to change notification settings - Fork 3
/
Library.sh
executable file
·185 lines (158 loc) · 3.42 KB
/
Library.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/bash
# ~/bin/Library.sh
#
# Source: https://github.com/vonbrownie/homebin/blob/master/Library.sh
set -eu
# A library of functions for bash shell scripts. Place script in ~/bin and call
# its functions by adding to a script ...
# . $HOME/bin/Library.sh
# ANSI escape codes
RED="\\033[1;31m"
GREEN="\\033[1;32m"
YELLOW="\\033[1;33m"
PURPLE="\\033[1;35m"
NC="\\033[0m" # no colour
L_echo_red() {
echo -e "${RED}$1${NC}"
}
L_echo_green() {
echo -e "${GREEN}$1${NC}"
}
L_echo_yellow() {
echo -e "${YELLOW}$1${NC}"
}
L_echo_purple() {
echo -e "${PURPLE}$1${NC}"
}
L_banner_begin() {
L_echo_yellow "\\n--------[ $1 ]--------\\n"
}
L_banner_end() {
L_echo_green "\\n--------[ $1 END ]--------\\n"
}
L_sig_ok() {
L_echo_green "\\n--> [ OK ]"
}
L_sig_fail() {
L_echo_red "\\n--> [ FAIL ]"
}
L_invalid_reply() {
L_echo_red "\\n'${REPLY}' is invalid input..."
}
L_invalid_reply_yn() {
L_echo_red "\\n'${REPLY}' is invalid input. Please select 'Y(es)' or 'N(o)'..."
}
L_penguin() {
cat << _EOF_
(O<
(/)_
_EOF_
}
L_run_script() {
while :
do
read -r -n 1 -p "Run script now? [yN] > "
if [[ "$REPLY" == [yY] ]]; then
echo -e "\\nLet's roll then ..."
sleep 4
break
elif [[ "$REPLY" == [nN] || "$REPLY" == "" ]]; then
L_echo_purple "\\n$( L_penguin )"
exit
else
L_invalid_reply_yn
fi
done
}
L_test_announce() {
L_echo_yellow "\\n$( L_penguin ) .: Let's first run a few tests ..."
}
L_test_root() {
local ERR="ERROR: script must be run with root privileges."
if (( EUID != 0 )); then
L_echo_red "\\n$( L_penguin ) .: $ERR"
exit 1
fi
}
L_test_internet() {
local ERR="ERROR: script requires internet access to do its job."
local UP
export UP
UP=$( nc -z 8.8.8.8 53; echo $? ) # Google DNS is listening?
if [[ "$UP" -ne 0 ]]; then
L_echo_red "\\n$( L_penguin ) .: $ERR"
exit 1
fi
}
L_test_required_file() {
local FILE=$1
local ERR="ERROR: file '$FILE' not found."
if [[ ! -f "$FILE" ]]; then
L_echo_red "\\n$( L_penguin ) .: $ERR"
exit 1
fi
}
L_test_homedir() {
# $1 is $USER
local ERR="ERROR: no USERNAME provided."
if [[ "$#" -eq 0 ]]; then
L_echo_red "\\n$( L_penguin ) .: $ERR"
exit 1
elif [[ ! -d "/home/$1" ]]; then
local ERR1="ERROR: a home directory for '$1' not found."
L_echo_red "\\n$( L_penguin ) .: $ERR1"
exit 1
fi
}
L_bak_file() {
for f in "$@"; do cp "$f" "$f.$(date +%FT%H%M%S).bak"; done
}
L_apt_update_upgrade() {
L_echo_yellow "\\nUpdate packages and upgrade $HOSTNAME ..."
apt update && apt -y full-upgrade
L_sig_ok
}
L_mktemp_dir_pwd() {
# Create a workspace directory within DIR
local DIR
DIR="$(pwd)"
local WORK_DIR
WORK_DIR=$( mktemp -d -p "$DIR" )
if [[ ! "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then
exit 1
fi
echo "$WORK_DIR"
}
L_mnt_mount() {
# $1 is sd[a-z][0-9] and $2 is MOUNTPOINT
local M_DEVICE
M_DEVICE="$( mount | grep "$1" | cut -d' ' -f1 )"
sudo mount "/dev/$1" "$2"
# confirm
if [[ ! $( L_mnt_detect "$1" ) ]]; then
L_sig_fail
exit 1
fi
}
L_mnt_umount() {
# $1 is sd[a-z][0-9]
local M_DEVICE
M_DEVICE="$( mount | grep "$1" | cut -d' ' -f1 )"
sudo umount "$M_DEVICE"
# confirm
if [[ $( L_mnt_detect "$1" ) ]]; then
L_sig_fail
exit 1
fi
}
L_debian_codename() {
lsb_release -c | awk -F: '{print $NF}' | sed 's/[[:blank:]]//g'
}
L_all_done() {
local MSG="All done!"
if [[ -x "/usr/games/cowsay" ]]; then
L_echo_green "$( /usr/games/cowsay "$MSG" )"
else
echo -e "$( L_penguin ) .: $MSG"
fi
}