ini-parser.bash - an ini-style parser written in bash for bash
ini-parser.bash reformats ini section headers so they can be interpreted by a shell.
The algorithm of translation from ini to shell is both tricky and simple.
Every single ini section is transformed into a shell function, and nothing more!
The algorithm acts as follow.
When an ini [section] is met, the algorithm first closes the previous section by adding a shell function close bracket }. Then it transforms the ini [section] into a shell function section() {
[section1]
key1=val1
key2=val2
[section2]
key3=val3
}
section1() {
key1=val1
key2=val2
}
section2() {
key3=val3
Finally, one more trick to make the translated shell valid. The algorithm moves the very first close bracket } at line 1 to the end.
section1() {
key1=val1
key2=val2
}
section2() {
key3=val3
}
A last trick is needed to handle empty sections. POSIX shells complain in case of an empty function. This empty function generates an error.
empty() {
}
To avoid complains the trick consists in adding the no-op colon instruction :, followed by the semi-colon instruction separator ; before closing the function.
empty() {
:;}
Check for the man-page, examples and limitations sections, singleline.ini, multiline.ini and variables.ini ini files used for tests.
The algorithm needs bash as only requirement.
It uses arrays to handle the file content line-by-line, and replacement substition.
Report bugs at https://github.com/gazoo74/templates/issues
Written by Gaël PORTAY gael.portay@savoirfairelinux.com
Widely inspired on work from Andrés J. Díaz.
Copyright (c) 2017 Gaël PORTAY
This program is free software: you can redistribute it and/or modify it under the terms of the MIT License.