aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'parse_config.sh')
-rwxr-xr-xparse_config.sh30
1 files changed, 30 insertions, 0 deletions
diff --git a/parse_config.sh b/parse_config.sh
new file mode 100755
index 0000000..24aba14
--- /dev/null
+++ b/parse_config.sh
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+# Bash INI file parser
+# Credit to ajdiaz
+# http://ajdiaz.wordpress.com/2008/02/09/bash-ini-parser/
+cfg_parser ()
+{
+ ini="$(<$1)" # read the file
+ ini="${ini//[/\[}" # escape [
+ ini="${ini//]/\]}" # escape ]
+ IFS=$'\n' && ini=( ${ini} ) # convert to line-array
+ ini=( ${ini[*]//;*/} ) # remove comments with ;
+ ini=( ${ini[*]/\ =/=} ) # remove tabs before =
+ ini=( ${ini[*]/=\ /=} ) # remove tabs be =
+ ini=( ${ini[*]/\ =\ /=} ) # remove anything with a space around =
+ ini=( ${ini[*]/#\\[/\}$'\n'cfg.section.} ) # set section prefix
+ ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1)
+ ini=( ${ini[*]/=/=\( } ) # convert item to array
+ ini=( ${ini[*]/%/ \)} ) # close array parenthesis
+ ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick
+ ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
+ ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
+ ini[0]="" # remove first element
+ ini[${#ini[*]} + 1]='}' # add the last brace
+ eval "$(echo "${ini[*]}")" # eval the result
+}
+
+
+cfg_parser $1
+cfg.section.vmconfig