GithubHelp home page GithubHelp logo

egberts / bash-ini-file Goto Github PK

View Code? Open in Web Editor NEW
11.0 0.0 0.0 69 KB

Get keyvalues by its section/keyword from an INI-format (v1.4) file in bash (also works with systemd, NetworkManager config files)

License: MIT License

Shell 100.00%
ini-parser ini-file ini-config ini-reader bash bash-script bash-scripting regex regex-pattern networkmanager-scripts

bash-ini-file's People

Contributors

egberts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bash-ini-file's Issues

Add --help to to a new main routine.

need to figure out how to display --help in the modularize bash script file.

it may entail upgrading this modularized bash script into an actual standalone script that can take argument(s) of files as well optionally displaying example usages then quit, using --help or -h command line option.

Help text body would display at least a few of the most useful examples.

Adding very simple usage examples may be practical

Hello,

Nice and helpful project!

I wanted to test this on my firefox profile, but had to struggle just a little bit until I found out how to use it:

nico@xantico:~$ source .local/bin/bash-ini-parser.sh 
nico@xantico:~$ cd .mozilla/firefox
nico@xantico:~/.mozilla/firefox$ ini_file_read profiles.ini 

nico@xantico:~/.mozilla/firefox$ cat profiles.ini 
[Install46F492E0ACFF84D4]
Default=s25g8abr.dev-edition-default
Locked=1

[Install4F96D1932A9F858E]
Default=waxl9k8u.default
Locked=1

[Profile1]
Name=default-release
IsRelative=1
Path=v6gmyi1u.default-release

[Profile0]
Name=default
IsRelative=1
Path=waxl9k8u.default
Default=1

[General]
StartWithLastProfile=1
Version=2

nico@xantico:~/.mozilla/firefox$ ini_section_list profiles.ini 

nico@xantico:~/.mozilla/firefox$ ini_file_read 

nico@xantico:~/.mozilla/firefox$ ini_file_read --help

nico@xantico:~/.mozilla/firefox$ ini_file_read `cat profiles.ini`

nico@xantico:~/.mozilla/firefox$ cat profiles.ini | ini_file_read 

nico@xantico:~/.mozilla/firefox$ ini_file_read "k1=val1"
[Default]k1=val1

nico@xantico:~/.mozilla/firefox$ ini_file_read "`cat profiles.ini`"
[Install46F492E0ACFF84D4]Default=s25g8abr.dev-edition-default
[Install46F492E0ACFF84D4]Locked=1
[Install4F96D1932A9F858E]Default=waxl9k8u.default
[Install4F96D1932A9F858E]Locked=1
[Profile1]Name=default-release
[Profile1]IsRelative=1
[Profile1]Path=v6gmyi1u.default-release
[Profile0]Name=default
[Profile0]IsRelative=1
[Profile0]Path=waxl9k8u.default
[Profile0]Default=1
[General]StartWithLastProfile=1
[General]Version=2
nico@xantico:~/.mozilla/firefox$

(Finally found out reading the tests). Maybe something like a simple

ini_file_read "`cat myfile.ini`"

(or whatever is better) somewhere in the README or from a --help option, might be nice.

Thank you anyway, this was much shorter than re-creating it entirely myself :-)

`//` inline comment not yet supported.

I know, I know, I know.... // inline comment is not yet supported.

But, but ... BUT I have a working Regex pattern for this. It is:

(((\x27[ \!\"\#\$\%\&\(\)\*\+\-\.\/0-9\:\;\<\=\>\?@A-Z\[\\\]\^\_\`a-z\|\~]*\x27\s*)|(\"[ \!\#\$\%\&\x27\(\)\*\+\-\.\/0-9\:\;\<\=\>\?@A-Z\[\\\]\^\_\`a-z\|\~]*\"\s*)|(\/([ \!\$\%\&\(\)\*\+\-\.0-9\:\<\=\>\?@A-Z\[\]\^\_\`a-z\|\~]+[ \!\$\%\&\(\)\*\+\-\.0-9\:\<\=\>\?@A-Z\[\]\^\_\`a-z\|\~]*)|([ \!\$\%\&\(\)\*\+\-\.0-9\:\<\=\>\?@A-Z\[\]\^\_\`a-z\|\~]*))*)*)([;#]+)*.*$

That regex has demonstrably worked on many online Regex testers:

Couldn't get it to work with the following online regex tester(s):

Just need a bit of major Bash-FU going on... and is on-going.

A Couple Suggestions

I was looking for bash handling of INI files rather than rolling my own and came across this which has some usable ideas.

However I find the returns on some of your functions to be backwards, for example:
ini_section_test returns 0 if the section doesn't exist and 1 if it does, which seems to got totally against posix shell conventions which is zero for true and non-zero for false.

to use your function as part of regular shell operations, the test has to be negated

if ! ini_section_test "${ini_data}" SectionName
then
    echo "Section Exists"
else
    echo "Section not Found"
fi

Which is highly counter intuitive: if not section exists then section exists??

Secondly I'd suggest being able to load the inifile into a global variable and never have to use it again, rather than passing ${ini_data} for every read or test, perhaps adding simple wrappers for the get/list functions that use the preloaded data. Just an idea...

Also generally INI files sections and keys are case-insensitive, not a deal breaker but an item of note.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.