-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmaster_functions.sh
78 lines (60 loc) · 1.29 KB
/
master_functions.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
#!/bin/bash
# Author: Jason Barnett <J@sonBarnett.com>
function ask_question {
local question
question=$1
read -r -p "$question: "
echo "${REPLY}"
}
function ask_yes_no {
local question answer
question=$1
read -r -p "$question: [y/n] "
answer=$(echo "${REPLY}" | downcase)
while [[ "${answer}" != "yes" && "${answer}" != "no" && "${answer}" != "y" && "${answer}" != "n" ]];do
read -r -p "y/n only please... $question: [y/n] "
answer=$(echo "${REPLY}" | downcase)
done
[[ "${answer}" == "yes" || "${answer}" == "y" ]] && echo true || echo false
}
function succ_or_fail {
# shellcheck disable=SC2181
if [[ $? == 0 ]]; then
msg success!
else
err_msg failed!
fi
}
function msg {
echo "$1"
}
function err_msg {
echo -e '\E[31m'"\033[1m${1}\033[0m" 1>&2
}
function fail_msg {
err_msg "${1}"
exit 1
}
function downcase {
tr '[:upper:]' '[:lower:]'
}
function upcase {
tr '[:lower:]' '[:upper:]'
}
function rstrip {
sed 's/[[:space:]]*$//' -
}
function lstrip {
sed 's/^[[:space:]]*//' -
}
function strip {
lstrip | rstrip
}
function gsub {
local pattern replacement
pattern=$1
replacement=$2
if [[ -n $pattern ]] && [[ -n $replacement ]]; then
sed "s|$pattern|$replacement|g"
fi
}