13

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.

Parker Coates
  • 8,520
  • 3
  • 31
  • 37
sorin
  • 161,544
  • 178
  • 535
  • 806
  • 1
    @fedorqui thanks, i improved the question, adding more information. – sorin Apr 03 '14 at 10:36
  • `.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 03 '14 at 11:25
  • I don't know if there is utility is available for ini file parsing but you can made your own utility in c++ Refer http://stackoverflow.com/questions/15647299/how-to-read-and-write-ini-files-using-boost-library This link show you how to read write init file using boost library in C++. – Rahul R Dhobi Apr 03 '14 at 10:57
  • [this question](http://stackoverflow.com/questions/16571739/bash-parsing-variables-from-config-file) might help get you started if you want to stick with the shell. – JoBu1324 Apr 03 '14 at 16:57

3 Answers3

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.

Parker Coates
  • 8,520
  • 3
  • 31
  • 37
  • 3
    In addition, git config appears not to allow underscores in option names. – Mike Spivey Apr 25 '20 at 08: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. – Alexis Wilke Nov 05 '20 at 05: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 09 '22 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"'
pixelbeat
  • 30,615
  • 9
  • 51
  • 60
  • 1
    python 3 support was just merged https://github.com/pixelb/crudini/pull/57 – pixelbeat Apr 11 '19 at 08: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. – Mike Spivey Apr 27 '20 at 22:42
  • 1
    You need to disclose the fact that you are the author of this package. – shrewmouse Dec 07 '20 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.

Bruno
  • 119,590
  • 31
  • 270
  • 376