Skip to content

Latest commit

 

History

History
97 lines (65 loc) · 2.17 KB

README.md

File metadata and controls

97 lines (65 loc) · 2.17 KB

ini-parser.bash(7)

NAME

ini-parser.bash - an ini-style parser written in bash for bash

DESCRIPTION

ini-parser.bash reformats ini section headers so they can be interpreted by a shell.

ALGORITHM

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() {
:;}

LINKS

Check for the man-page, examples and limitations sections, singleline.ini, multiline.ini and variables.ini ini files used for tests.

REQUIREMENT

The algorithm needs bash as only requirement.

It uses arrays to handle the file content line-by-line, and replacement substition.

BUGS

Report bugs at https://github.com/gazoo74/templates/issues

AUTHOR

Written by Gaël PORTAY [email protected]

Widely inspired on work from Andrés J. Díaz.

COPYRIGHT

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.