-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic.bash
76 lines (72 loc) · 1.86 KB
/
basic.bash
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
#!/bin/bash
# @ writelog
# Description: Append a message to a logfile
# 1st param : Text to append to the logfile. Default value /tmp/bash-toolbox.log
# 2nd param : Log file path. Default value /tmp/bash-toolbox.log
function writelog {
local text="${1}"
local logfile="${2}"
if [ -n "${text}" ]; then
if [ -z "${logfile}" ]; then
logfile='/tmp/bash-toolbox.log'
fi
echo "$(date '+%Y-%m-%d %H:%M:%S') - ${text}" >> "${logfile}"
fi
}
# @ showinscreen
# Description: Prints a colored message to screen
# 1st param : Text to print
# 2nd param : [optional] Message level (ERR,WRN,INF,OK)
function showinscreen {
local red='\033[1;31m'
local green='\033[0;32m'
local yellow='\033[1;33m'
local blue='\033[1;34m'
local default='\033[0m'
local color="${default}"
local msgText="${1}"
local msgType="${2}"
if [ -z "${msgType}" ]; then
color="${default}"
elif [ "${msgType}" = "ERR" ]; then
color="${red}"
elif [ "${msgType}" = "WRN" ]; then
color="${yellow}"
elif [ "${msgType}" = "INF" ]; then
color="${blue}"
elif [ "${msgType}" = "OK" ]; then
color="${green}"
fi
echo -e "${color}${msgText}${default}"
}
# @ printListItem
# Description: Prints a 'list item' character
# 1st param : List item level (indentation in the list)
function printListItem {
local indent=' '
local lv1='o'
local lv2='>'
local lv3='*'
local lv4='-'
local lv5='.'
local line=''
local char=''
{
local let itemlevel="${1}"
} || local let itemlevel=1
for level in $(seq 1 ${itemlevel}); do
if [ ${level} -eq 1 ]; then
char="${lv1}"
elif [ ${level} -eq 2 ]; then
char="${lv2}"
elif [ ${level} -eq 3 ]; then
char="${lv3}"
elif [ ${level} -eq 4 ]; then
char="${lv4}"
elif [ ${level} -gt 4 ]; then
char="${lv5}"
fi
line="${line}${indent}"
done
echo -n "${line}${char} "
}