~kolletzki/bashcfg

A bash function for parsing config files
Add build manifest
Fix example output in README
Create parent directories, if needed

refs

main
browse  log 

clone

read-only
https://git.sr.ht/~kolletzki/bashcfg
read/write
git@git.sr.ht:~kolletzki/bashcfg

You can also use your local clone with git send-email.

#bashcfg

A bash function for parsing config files.

Sometimes, when bash scripts grow, you'll find yourself in the situation where you want to have definitely need a config file. But of course, you being the nerd that writes too complex advanced bash scripts, you're not interested in a third-party tool for parsing said config files.

bashcfg to the rescue!

It's a small(ish) bash function that you can add to your script to parse a config file.

#Usage

The script does not depend on any external tools except for a "modern" bash version. Make sure you have bash >= 4 installed (esp. if you're on macOS).

Take a look at bashcfg.sh file and copy the function and related variables. Read the code and adjust as necessary.

#Config file format

  • All values are strings
  • First = sign splits the key and the value
  • Leading and trailing whitespace of keys and values are trimmed (whitespace around = is allowed)
  • Lines beginning with a # (whitespace ignored) are comments
  • Empty lines are ignored
  • Should be fairly safe (does not execute subshells, etc.)
  • Empty values are respected
  • Lines without a delimiter (=) are ignored

#Example

myscript.conf

foo=bar
foo2 = bar2



# This is a comment
  # indented comment
#another comment

  indented = config line

some_more_weirdness    =    lots of spaces

value_with_trailing_whitespace = value with trailing whitespace   

empty=

evil = $(ping 9.9.9.9)
$(ping 1.1.1.1)

output

foo has the value 'bar'
foo2 has the value 'bar2'
indented has the value 'config line'
some_more_weirdness has the value 'lots of spaces'
value_with_trailing_whitespace has the value 'value with trailing whitespace'
empty has the value ''
evil has the value '$(ping 9.9.9.9)'
bar has the value 'baz'

#Contribute and get help

You are more than welcome to contribute to this project. Please send patches to ~kolletzki/patches@lists.sr.ht. Thanks!

If you have any questions, feel free to drop a line to my public inbox.

#License

This script is released as public domain under the Unlicense.

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
Do not follow this link