Ini-Config file parser for bash.
- Same section & key pair will override its ancestors.
- Lines starting with
#
or;
will be ignored. - Empty lines will be ignored.
- Values with no keys will be ignored.
- At the beginning of the config file, keys with unspecified sections will automatically be added to the
Fallback
section. - Sections & keys can only contain alpha-numeric characters and underscores.
- Sections & keys are case sensitive (by default).
- Leading/trailing blanks will be removed.
- Punctuation characters inside keys/sections will be converted to underscores
_
internally (sofoo.bar
will be identical tofoo_bar
). - Multiple underscores inside keys/sections will be removed internally (e.g.
__some_key__
to_some_key_
). - Comments inside values will be removed.
Funcitons | Argument(s) | Optional argument(s) | Description |
---|---|---|---|
ini_process_file |
"file" | - | Process the specified config file. |
ini_get_value |
"section", "key" | - | Return a value from specified section & key pair. |
ini_display |
none | - | Fully display the processed file. |
ini_print_keys |
"section" | "boolean" | Display the keys with or without indexes of a specified section. |
ini_print_values |
"section" | "boolean" | Display the values with or without indexes of a specified section. |
ini_display_by_section |
"section" | - | Display a specified section. |
There is a complete example available (parsing-example.sh).
Globals | Type | Default value |
---|---|---|
INI_CASE_SENSITIVE_SECTIONS |
boolean | true |
INI_CASE_SENSITIVE_KEYS |
boolean | true |
INI_SHOW_WARNINGS |
boolean | true |
INI_SHOW_ERRORS |
boolean | true |
INI_RAW_MODE |
boolean | false |
- In raw mode, all values will be interpreted as plain text. This means, there will be no variable expansions. (e.g.
$HOME
will be$HOME
or\t
would still be\t
etc.)
Other than the above mentioned globals, there are also default variables.
Defaults | Type | Value | Value Changes With | Value Changes to |
---|---|---|---|---|
INI_DEFAULT_PRINTF |
*Hardcoded | "printf" | INI_RAW_MODE |
"printf %s" |
INI_DEFAULT_SECTION |
*Hardcoded | "Fallback" | - | - |
However, changing them may result with undefined behaviour.
(* Hardcoded: To change the value, one should modify the library.)
#!/bin/bash
# Turn off warnings
INI_SHOW_WARNINGS=false
# Case insensitive section names
INI_CASE_SENSITIVE_KEYS=false
# Load the parser
source libsh-iniparser.sh
# Process the config file
ini_process_file 'example.conf'
# Display a specific value from a specific section
ini_get_value 'section1' 'key1'
#!/bin/bash
# Load the parser
source libsh-iniparser.sh
# Process the config file
ini_process_file 'example.conf'
# Display a specific value from a specific section
ini_get_value 'section1' 'key1'
- Two arrays per section will be generated. One for keys and one for values.
- Both of these arrays are assumed to be the same length. Any loss of synchronization will cause fatal failure.
- For a more secure approach,
#!/bin/bash
is used as SheBang. If this causes problems, please runwhich bash
and change the SheBang accordingly.
libsh-iniparser since 59e6753 is sublicensed under the LGPL v3. See LICENSE for details.
A relevant section of the original version's license is below:
Copyright © `2009-2021` `Wolf Software`
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
Note: I have no affiliation with Wolf Software.
Copyright (C) <2022> Fatih Yeğin
Copyright (C) <2009-2021> Wolf Software