14

I am looking for a generic command line solution that would allow me to add or modify a configuration option inside a config file (INI-like format).

Most Linux configuration files use a INI-like format, with # and ; as comment and with option=value.

Mainly I am looking for something that would take filename, option and value, and that will change the config to match this.

I want to use this to write some auto-deployment scripts. I have no problem on using tools that are not installed by default on Debian or Ubuntu as long they do exist in the default distribution repositories (as I can do an apt-get install xxx, if needed).

Example: change-config /etc/default/nginx ULIMIT '"-n 4096"'

The expected result would be to have ULIMIT="-n 4096" inside the nginx file. Obviously if it does already exists and have the same value, it should do nothing. If it exists, commenting the old line would be fine and adding the new one.

As a note, these config files can have spaces/tabs between parameters so if you have ULIMIT = "..." is still the same command. That's why I was looking for something better than sed as there are plenty of corner cases to evaluate.

Also, I don't want to reinvent the wheel, and I doubt that I am the first one to look for a solution to this kind of problem.

4
  • 1
    @fedorqui thanks, i improved the question, adding more information.
    – sorin
    Apr 3, 2014 at 10:36
  • I don't know if there is utility is available for ini file parsing but you can made your own utility in c++ Refer stackoverflow.com/questions/15647299/… This link show you how to read write init file using boost library in C++. Apr 3, 2014 at 10:57
  • .ini files usually have non-unix-util-friendly layouts. But what you describe sounds like a typical unix env or .rc file. There are no specific tools to do exactly what you're asking for, but it is a small shell script to do so. Read about bash, sed and/or awk. Also don't plan on using configure for your script name, as that name is a well-known script that is part of a unix-standard install pattern, i.e. configure; make; make install . Good luck.
    – shellter
    Apr 3, 2014 at 11:25
  • this question might help get you started if you want to stick with the shell.
    – JoBu1324
    Apr 3, 2014 at 16:57

4 Answers 4

22

git config is actually a semi-generic INI interface.

❱ git config --file=/etc/default/nginx somegroup.ULIMIT '-n 4096'
❱ cat /etc/default/nginx
[somegroup]
    ULIMIT = -n 4096
❱ git config --file=/etc/default/nginx somegroup.ULIMIT
"-n 4096"

It doesn't support adding top-level keys, though. All keys have to be placed in an INI style group, hence the "somegroup." above. That makes it unsuitable for your task, but I thought I'd mention it here for others finding their way here.

3
  • 3
    In addition, git config appears not to allow underscores in option names. Apr 25, 2020 at 8:52
  • 1
    Wow! That works. Weird tool to do such edits, but somehow git is always installed on the target machines in my case, so it's just perfect. Nov 5, 2020 at 5:18
  • Great solution and I thought I was onto something for my use case, but sadly git config will not accept underscore as a part of a setting name.
    – KenB
    Nov 9, 2022 at 17:46
8

Try crudini. BTW I think this file is a shell file rather than an ini file, but crudini can still work in this case:

crudini --set /etc/default/nginx '' ULIMIT '"-n 4096"'
3
  • 1
    python 3 support was just merged github.com/pixelb/crudini/pull/57
    – pixelbeat
    Apr 11, 2019 at 8:29
  • Another gotcha: crudini preserved the spacing around equals signs in existing lines, but insists on surrounding the = with spaces when creating new entries. Some other programs cannot parse the INI files that result from this. Apr 27, 2020 at 22:42
  • 1
    You need to disclose the fact that you are the author of this package.
    – shrewmouse
    Dec 7, 2020 at 16:28
2

Augeas / augtool aims to do this, although you'll need the right lens for the type of file you're after (you can also write your own), for example, the Nginx lens.

It also has an API if required.

0

You can perform that using python as described in this related question Change value in ini file using ConfigParser Python.

While python is installed already in almost every Linux distribution, so you won't need extra tools :-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.