|
| 1 | +<p align="center"> |
| 2 | + <a href="https://github.com/DevelopersToolbox/"> |
| 3 | + <img src="https://cdn.wolfsoftware.com/assets/images/github/organisations/developerstoolbox/black-and-white-circle-256.png" alt="DevelopersToolbox logo" /> |
| 4 | + </a> |
| 5 | + <br /> |
| 6 | + <a href="https://github.com/DevelopersToolbox/ini-file-parser/actions/workflows/pipeline.yml"> |
| 7 | + <img src="https://img.shields.io/github/workflow/status/DevelopersToolbox/ini-file-parser/pipeline/master?style=for-the-badge" alt="Github Build Status"> |
| 8 | + </a> |
| 9 | + <a href="https://github.com/DevelopersToolbox/ini-file-parser/releases/latest"> |
| 10 | + <img src="https://img.shields.io/github/v/release/DevelopersToolbox/ini-file-parser?color=blue&label=Latest%20Release&style=for-the-badge" alt="Release"> |
| 11 | + </a> |
| 12 | + <a href="https://github.com/DevelopersToolbox/ini-file-parser/releases/latest"> |
| 13 | + <img src="https://img.shields.io/github/commits-since/DevelopersToolbox/ini-file-parser/latest.svg?color=blue&style=for-the-badge" alt="Commits since release"> |
| 14 | + </a> |
| 15 | + <br /> |
| 16 | + <a href=".github/CODE_OF_CONDUCT.md"> |
| 17 | + <img src="https://img.shields.io/badge/Code%20of%20Conduct-blue?style=for-the-badge" /> |
| 18 | + </a> |
| 19 | + <a href=".github/CONTRIBUTING.md"> |
| 20 | + <img src="https://img.shields.io/badge/Contributing-blue?style=for-the-badge" /> |
| 21 | + </a> |
| 22 | + <a href=".github/SECURITY.md"> |
| 23 | + <img src="https://img.shields.io/badge/Report%20Security%20Concern-blue?style=for-the-badge" /> |
| 24 | + </a> |
| 25 | + <a href="https://github.com/DevelopersToolbox/ini-file-parser/issues"> |
| 26 | + <img src="https://img.shields.io/badge/Get%20Support-blue?style=for-the-badge" /> |
| 27 | + </a> |
| 28 | + <br /> |
| 29 | + <a href="https://wolfsoftware.com/"> |
| 30 | + <img src="https://img.shields.io/badge/Created%20by%20Wolf%20Software-blue?style=for-the-badge" /> |
| 31 | + </a> |
| 32 | +</p> |
| 33 | + |
| 34 | +## Overview |
| 35 | + |
| 36 | +Bash does not come with a way to process ini/config files, this script is an attempt at creating a generic easy to use solution to this problem and for no other reason than because I could. |
| 37 | + |
| 38 | +## How it works |
| 39 | + |
| 40 | +The script works by reading in a normal ini file and creates 2 arrays per section. One for the keys and one for the values. The reason of this is that you cannot declare an associative array globally from within a loop which is within a function (in bash 4.3 at least) so this was the best work around that was available. |
| 41 | + |
| 42 | +## Example ini file |
| 43 | + |
| 44 | +There are 2 config files provided as examples. |
| 45 | + |
| 46 | +| Name | Description | |
| 47 | +| --- | --- | |
| 48 | +| [simple example](tests/simple-example.conf) | Simple config file, demonstrating sections and key=value key pairs. | |
| 49 | +| [complete example](tests/complete-example.conf) | Complex config file, demonstrating all of the processing rules, warnings and error conditions.| |
| 50 | + |
| 51 | +### Simple config file |
| 52 | +``` |
| 53 | +[section1] |
| 54 | +value1=abcd |
| 55 | +value2=1234 |
| 56 | +
|
| 57 | +[section2] |
| 58 | +value1=1234 |
| 59 | +value2=5678 |
| 60 | +
|
| 61 | +[section3] |
| 62 | +value1=abcd |
| 63 | +value2=1234 |
| 64 | +value3=a1b2 |
| 65 | +value_4=c3d4 |
| 66 | +``` |
| 67 | + |
| 68 | +## Example usage |
| 69 | + |
| 70 | +There is a complete working example available, [parse-example.sh](tests/parse-example.sh), but a 'snippet' example is given below, to show a simple single value extraction. |
| 71 | + |
| 72 | +```shell |
| 73 | +#!/usr/bin/env bash |
| 74 | + |
| 75 | +# Load in the ini file parser |
| 76 | +source ini-file-parser.sh |
| 77 | + |
| 78 | +# Load and process the ini/config file |
| 79 | +process_ini_file 'example.conf' |
| 80 | + |
| 81 | +# Display a specific value from a specific section |
| 82 | +echo $(get_value 'section1' 'value1') |
| 83 | + |
| 84 | +# Display the same value as above but using the global variables created as part of the processing. |
| 85 | +echo $section1_value1 |
| 86 | +``` |
| 87 | + |
| 88 | +## Processing rules |
| 89 | + |
| 90 | +There are a number of rules / assumptions that have been coded in that might give you a result different to the one you might expect: |
| 91 | + |
| 92 | +1. Empty lines are ignored. |
| 93 | +2. Lines starting with comments (# or ;) are ignored. |
| 94 | +3. The 'default' section is called 'default' - This is for any name=value pair defined before the first section. |
| 95 | +4. Section names will only contains alpha-numberic characters and underscores - everything else is removed. |
| 96 | +5. Section names are case sensitive (by default). |
| 97 | +6. Key names have leading and trailing spaces removed. |
| 98 | +7. Key names replace all punctuation and blank characters (space, tab etc) with underscore and squish (remove multiple underscores). |
| 99 | +8. Value fields have in-line comments removed. |
| 100 | +9. Value fields have leading and trailing spaces removed. |
| 101 | +10. Value fields have leading and trailing quotes removed. |
| 102 | + |
| 103 | +In attition to the data arrays that are created a set of variables are also creted to allow for simple direction access to values, these are in the format: section_key=value. |
| 104 | + |
| 105 | +It is worth noting that the global variables are created AFTER the processing rules have been applied, so the section name and the key name might differ from what you expect. |
| 106 | + |
| 107 | +## Subroutines |
| 108 | + |
| 109 | +There are currently 4 subroutines that are exposed. |
| 110 | + |
| 111 | +1. process\_ini\_file - Load a names ini/config file and create the required arrays. |
| 112 | +2. get\_value - Request a specific value (by key name) from a specific section. |
| 113 | +3. disply\_config - Display the processed (clean) ini/config in ini/config file format. |
| 114 | +4. display\_config\_by\_section - As above but for a specific named section. |
| 115 | + |
| 116 | +## Global overrides |
| 117 | + |
| 118 | +These variables allow us to override the parse script defaults, they can be used by the calling script, but they must be set **BEFORE** the ini/config file is processed. |
| 119 | + |
| 120 | +| Name | Description | Default | Accepted Values | |
| 121 | +| --- | --- | --- | --- | |
| 122 | +| case\_sensitive\_sections | Should section names be case sensitive? | true | true / false | |
| 123 | +| case\_sensitive\_keys | Should key names be case sensitive? | true | true / false | |
| 124 | +| show\_config\_warnings | Should we show config warnings? | true | true / false | |
| 125 | +| show\_config\_errors | Should we show config errors? | true | true / false | |
| 126 | + |
| 127 | +### Example override usage |
| 128 | + |
| 129 | +```shell |
| 130 | +#!/usr/bin/env bash |
| 131 | + |
| 132 | +# Turn off config warnings |
| 133 | +show_config_warnings=false |
| 134 | + |
| 135 | +# Make Section names case-insensitive |
| 136 | +case_sensitive_sections=false |
| 137 | + |
| 138 | +# Load in the ini file parser |
| 139 | +source ini-file-parser.sh |
| 140 | + |
| 141 | +# Load and process the ini/config file |
| 142 | +process_ini_file 'example.conf' |
| 143 | + |
| 144 | +# Display a specific value from a specific section |
| 145 | +echo $(get_value 'section1' 'value1') |
| 146 | + |
| 147 | +# Display a value using the global variable created as part of the processing. |
| 148 | +echo $section1_value1 |
| 149 | +``` |
0 commit comments