-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
parse-example.sh
executable file
·135 lines (108 loc) · 6.84 KB
/
parse-example.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
#!/usr/bin/env bash
# -------------------------------------------------------------------------------- #
# Description #
# -------------------------------------------------------------------------------- #
# This is a very simple (almost trivial) example of how to make use of the #
# os-detect script and embed os detection into your scripts. #
# -------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------- #
# Declaration #
# -------------------------------------------------------------------------------- #
# Declare variables that we are going to use later, these are variables that are #
# dynamically created by the parsing script. This isn't 100% needed but it is #
# cleaner to do it this way and also makes sure that shellcheck is 100% clean. #
# -------------------------------------------------------------------------------- #
declare sections
declare section1_value1
declare section1_keys
declare section1_values
declare SECTION1_VALUE1
declare SECTION1_keys
declare SECTION1_values
# -------------------------------------------------------------------------------- #
# Global Overrides #
# -------------------------------------------------------------------------------- #
# These variables allow us to override the parse script defaults. #
# #
# case_sensitive_sections - should section names be case sensitive #
# case_sensitive_keys - should key names be case sensitive #
# default_to_uppercase - should we default to uppercase? #
# show_config_warnings - should we show config warnings #
# show_config_errors - should we show config errors #
# #
# You can uncomment either (or both) of the values below to override the default #
# value of true. #
# -------------------------------------------------------------------------------- #
export case_sensitive_sections=false
export case_sensitive_keys=false
export default_to_uppercase=true
#export show_config_warnings=false
#export show_config_errors=false
# -------------------------------------------------------------------------------- #
# Use the source #
# -------------------------------------------------------------------------------- #
# Source the os-detect script to make the variables available. #
# -------------------------------------------------------------------------------- #
SCRIPTPATH="$( dirname "$( cd "$(dirname "$0")" >/dev/null 2>&1 || exit ; pwd -P )" || true)"
# shellcheck disable=SC1090,SC1091
source "${SCRIPTPATH}"/src/ini-file-parser.sh
# -------------------------------------------------------------------------------- #
# Profile ini file #
# -------------------------------------------------------------------------------- #
# Tell the parser which ini file to process. #
# -------------------------------------------------------------------------------- #
process_ini_file 'complete-example.conf'
# -------------------------------------------------------------------------------- #
# Display Config #
# -------------------------------------------------------------------------------- #
# Tell the parser to display ALL of the loaded and parsed config. #
# -------------------------------------------------------------------------------- #
echo "Display Config"
display_config
# -------------------------------------------------------------------------------- #
# Display Config bu Section #
# -------------------------------------------------------------------------------- #
# Tell the parser to display ALL of the loaded and parsed config for a specific #
# named section. #
# -------------------------------------------------------------------------------- #
echo "Display Section 2"
display_config_by_section 'section2'
# -------------------------------------------------------------------------------- #
# Get Value #
# -------------------------------------------------------------------------------- #
# Retrieve a specific value from a specific section. #
# -------------------------------------------------------------------------------- #
echo "Display Section 1 - Value 1 (get_value lookup)"
value=$(get_value 'section1' 'value1')
echo "${value}"
# -------------------------------------------------------------------------------- #
# Dynamic Variables #
# -------------------------------------------------------------------------------- #
# Use a dynamicalled created variable directly. #
# -------------------------------------------------------------------------------- #
echo "Display Section 1 - Value 1 (Named variable)"
if [[ "${default_to_uppercase}" = false ]]; then
echo "${section1_value1}"
else
echo "${SECTION1_VALUE1}"
fi
# -------------------------------------------------------------------------------- #
# Section, Key and Value Traversals #
# -------------------------------------------------------------------------------- #
# Traverse sections, keys and values of the section. #
# -------------------------------------------------------------------------------- #
echo
echo "Display Section, Key and Value Traversals"
echo "${sections[@]}"
if [[ "${default_to_uppercase}" = false ]]; then
echo "${section1_keys[@]}"
echo "${section1_values[@]}"
else
echo "${SECTION1_keys[@]}"
echo "${SECTION1_values[@]}"
fi
# -------------------------------------------------------------------------------- #
# End of Script #
# -------------------------------------------------------------------------------- #
# This is the end - nothing more to see here. #
# -------------------------------------------------------------------------------- #