-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.sh
executable file
·83 lines (76 loc) · 1.54 KB
/
utils.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
#!/usr/bin/env bash
#
EXIT_SUCCESS=0
EXIT_ABNORMAL=2
TRUE=0
FALSE=1
percentage(){
local var1="$1"
local var2="$2"
if [ "$var2" -le 0 ];then
echo "0.00%"
return "${EXIT_ABNORMAL}"
fi
declare result
local awk_script="{ printf( \"%3.2f%%\n\", ($1/$2)*100 ) }"
result=$(echo "$var1" "$var2" | env awk "$awk_script")
echo "$result"
return "${EXIT_SUCCESS}"
}
#
is_over_used(){
local usage=$1
local total=$2
_is_high=$(bc << EOF
scale = 2
benchmark = .60
quotient = $usage / $total
define flag(q){
if (q < benchmark) return $FALSE
return $TRUE
}
flag(quotient)
EOF
)
echo "$_is_high"
return "${EXIT_SUCCESS}"
}
_warn(){
echo -e "\033[35mWARNING:\033[0m ${1}"
return "${EXIT_SUCCESS}"
}
echoh(){
env echo -e "$@"
return "${EXIT_SUCCESS}"
}
prepend_path(){
for p in $*
do
readable "${p}"
exists=$?
echo "${PATH}" | grep -q "${p}" 2>/dev/null
in_path=$?
if [[ $exists -eq $TRUE && $in_path -ne $TRUE ]];then
export PATH="${p}:${PATH}"
fi
done
}
prepend_pythonpath(){
for p in $*
do
readable "${p}"
exists=$?
echo "${PYTHONPATH}" | grep -q "${p}" 2>/dev/null
in_path=$?
if [[ $exists -eq $TRUE && $in_path -ne $TRUE ]];then
export PYTHONPATH="${p}:${PYTHONPATH}"
fi
done
}
readable(){
if [[ "${1}" == "" || ! -r "${1}" ]];then
_warn "${1} doesn't exist!"
return ${FALSE}
fi
return ${TRUE}
}