-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutilities.sh
executable file
·124 lines (104 loc) · 2.6 KB
/
utilities.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
#!/bin/bash
SILENT_MODE=0
SPIN[0]="-"
SPIN[1]="\\"
SPIN[2]="|"
SPIN[3]="/"
loading() {
local message=${1}
while true; do
for s in "${SPIN[@]}"; do
clear_print "[${message}] ${s}"
sleep 0.1
done
done
} # loading()
print_help() {
println "${USAGE}"
println "${HELP_TEXT}"
} # show_help()
write() {
if [ "${SILENT_MODE}" == "0" ]; then
if [ $# -ge 2 ]; then
printf "${1}" "${2}"
elif [ $# -eq 1 ]; then
printf "${1}"
fi
fi
} # message()
println() {
write "${1}\n"
} # println()
printfln() {
write "%s\n" "${1}"
} # printfln()
clear_print() {
write "\r$(tput el)"
write "${1}"
} # clear_print()
clear_println() {
clear_print "${1}"
write "\n"
} # clear_println()
clear_printf() {
write "\r$(tput el)"
write "%s" "${1}"
} # clear_printf()
clear_printfln() {
clear_printf "${1}"
write "\n"
} # clear_printfln()
std_err() {
println "${1}" 1>&2
log "ERROR" "${1}"
} # std_err()
std_ferr() {
printfln "${1}" 1>&2
log "ERROR" "${1}"
} # std_ferr()
log() {
[ $# -eq 2 ] || return
[ ${DEBUG_MODE} -eq 1 ] || return
[ -z "${LOG_DIR}" ] || [ -z "${LOG_FILE}" ] && return
local tag="${1}"
local msg="${2}"
[ -z "${msg}" ] && return
[ -d ${LOG_DIR} ] || mkdir ${LOG_DIR}
printf "\r$(tput el)"
printf "$(date) - [${tag}]:\n%s\n\n%s\n\n" "${msg}" "------------------------" >> ${LOG_DIR}/${LOG_FILE}
} # log()
argument_parameter_exists() {
local index="${1}"
index=$((index + 2))
if [ ${index} -gt $(($#)) ] \
|| [ $(expr "${!index}" : "^-.*$") -gt 0 ] \
|| [ $(expr "${!index}" : "^$") -gt 0 ]; then
return 1
else
return 0
fi
} # check_option_value()
prompt_root() {
# @description This function prompts the user for their root password and stores
# the result in 'ROOT_PASSWORD', if function fails then 'ROOT_PASSWORD' is
# cleared
#
# @return If the user fails to give the correct password 3 times the function
# returns 1, if succeeds then 0
local failed_count=0
while true; do
read -s -p "[sudo] password for $(whoami): " ROOT_PASSWORD
println ""
println "${ROOT_PASSWORD}" | sudo -k -S -s ls &>/dev/null
if [ $? -eq 0 ]; then
return 0
else
failed_count=$((failed_count + 1))
if [ ${failed_count} -ge 3 ]; then
std_err "sudo: 3 incorrect password attempts"
return 1
fi
println "Sorry, try again."
fi
done
} # prompt_root()