ini-file-parser

A bash implementation of an ini (config) file parser.

https://github.com/developerstoolbox/ini-file-parser

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.0%) to scientific vocabulary

Keywords

ini-parser wolfsoftware
Last synced: 4 months ago · JSON representation ·

Repository

A bash implementation of an ini (config) file parser.

Basic Info
  • Host: GitHub
  • Owner: DevelopersToolbox
  • License: mit
  • Language: Shell
  • Default Branch: master
  • Homepage:
  • Size: 209 KB
Statistics
  • Stars: 48
  • Watchers: 5
  • Forks: 10
  • Open Issues: 3
  • Releases: 7
Topics
ini-parser wolfsoftware
Created almost 5 years ago · Last pushed 4 months ago
Metadata Files
Readme Contributing Funding License Code of conduct Citation Codeowners Security

README.md

DevelopersToolbox logo
Github Build Status License Created
Release Released Commits since release

Overview

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.

How it works

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.

Example ini file

There are 2 config files provided as examples.

| Name | Description | | --- | --- | | simple example | Simple config file, demonstrating sections and key=value key pairs. | | complete example | Complex config file, demonstrating all of the processing rules, warnings and error conditions.|

Simple config file

``` [section1] value1=abcd value2=1234

[section2] value1=1234 value2=5678

[section3] value1=abcd value2=1234 value3=a1b2 value_4=c3d4 ```

Example usage

There is a complete working example available, parse-example.sh, but a 'snippet' example is given below, to show a simple single value extraction.

```shell

!/usr/bin/env bash

Load in the ini file parser

source ini-file-parser.sh

Load and process the ini/config file

processinifile 'example.conf'

Display a specific value from a specific section

echo $(get_value 'section1' 'value1')

Display the same value as above but using the global variables created as part of the processing.

echo $section1_value1 ```

Processing rules

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:

  1. Empty lines are ignored.
  2. Lines starting with comments (# or ;) are ignored.
  3. The 'default' section is called 'default' - This is for any name=value pair defined before the first section.
  4. Section names will only contains alpha-numeric characters and underscores - everything else is removed.
  5. Section names are case sensitive (by default).
  6. Key names have leading and trailing spaces removed.
  7. Key names replace all punctuation and blank characters (space, tab etc) with underscore and squish (remove multiple underscores).
  8. Value fields have in-line comments removed.
  9. Value fields have leading and trailing spaces removed.
  10. Value fields have leading and trailing quotes removed.

In addition to the data arrays that are created a set of variables are also created to allow for simple direction access to values, these are in the format: section_key=value.

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.

Subroutines

There are currently 5 subroutines that are exposed.

  1. process_ini_file - Load a names ini/config file and create the required arrays.
  2. get_value - Request a specific value (by key name) from a specific section.
  3. display_config - Display the processed (clean) ini/config in ini/config file format.
  4. display_config_by_section - As above but for a specific named section.
  5. global_reset - Reset the script and remove all previously loaded ini file data.

Global overrides

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.

| Name | Description | Default | Accepted Values | | --- | --- | --- | --- | | case_sensitive_sections | Should section names be case sensitive? | true | true / false | | case_sensitive_keys | Should key names be case sensitive? | true | true / false | | show_config_warnings | Should we show config warnings? | true | true / false | | show_config_errors | Should we show config errors? | true | true / false |

Example override usage

```shell

!/usr/bin/env bash

Turn off config warnings

showconfigwarnings=false

Make Section names case-insensitive

casesensitivesections=false

Load in the ini file parser

source ini-file-parser.sh

Load and process the ini/config file

processinifile 'example.conf'

Display a specific value from a specific section

echo $(get_value 'section1' 'value1')

Display a value using the global variable created as part of the processing.

echo $section1_value1 ```


Owner

  • Name: Developers Toolbox
  • Login: DevelopersToolbox
  • Kind: organization
  • Email: github@wolfsoftware.com
  • Location: United Kingdom

A selection of tools to help developers. Created by Wolf Software.

Citation (CITATION.cff)

cff-version: 1.2.0
message: If you use this software, please cite it using these metadata.
title: Ini File Parser
abstract: A bash implementation of an ini (config) file parser.
type: software
version: 0.1.4
date-released: 2024-06-04
repository-code: https://github.com/DevelopersToolbox/ini-file-parser
keywords:
  - "Wolf Software"
  - "Software"
license: MIT
authors:
  - family-names: "Wolf"
    orcid: "https://orcid.org/0009-0007-0983-2072"

GitHub Events

Total
  • Create event: 63
  • Release event: 1
  • Issues event: 2
  • Watch event: 6
  • Delete event: 63
  • Issue comment event: 128
  • Push event: 123
  • Pull request review event: 108
  • Pull request event: 130
  • Fork event: 2
Last Year
  • Create event: 63
  • Release event: 1
  • Issues event: 2
  • Watch event: 6
  • Delete event: 63
  • Issue comment event: 128
  • Push event: 123
  • Pull request review event: 108
  • Pull request event: 130
  • Fork event: 2

Issues and Pull Requests

Last synced: 4 months ago

All Time
  • Total issues: 6
  • Total pull requests: 86
  • Average time to close issues: 7 days
  • Average time to close pull requests: 7 days
  • Total issue authors: 5
  • Total pull request authors: 3
  • Average comments per issue: 5.0
  • Average comments per pull request: 1.73
  • Merged pull requests: 74
  • Bot issues: 0
  • Bot pull requests: 84
Past Year
  • Issues: 1
  • Pull requests: 68
  • Average time to close issues: 2 days
  • Average time to close pull requests: 1 day
  • Issue authors: 1
  • Pull request authors: 2
  • Average comments per issue: 3.0
  • Average comments per pull request: 1.68
  • Merged pull requests: 57
  • Bot issues: 0
  • Bot pull requests: 67
Top Authors
Issue Authors
  • TBarregren (2)
  • rayee-github (1)
  • egberts (1)
  • vollmeier-dev (1)
  • dependabot[bot] (1)
  • ThaDaVos (1)
Pull Request Authors
  • dependabot[bot] (132)
  • TGWolf (2)
  • JohanChane (2)
Top Labels
Issue Labels
impact: medium (2) state: accepted (2) state: in progress (2) type: bug (2) state: triage (2) state: complete (1) state: confirmed (1) type: question (1) type: enhancement (1) dependabot: dependencies (1) dependabot: ecosystem : github actions (1) dependabot: auto approve (1) dependabot: auto merge (1)
Pull Request Labels
dependabot: dependencies (132) dependabot: ecosystem : github actions (132) dependabot: auto approve (121) dependabot: auto merge (121) dependabot: manual merge (3)