A simple linux command line command to change an option in a configuration file like INI

I am looking for a universal command line solution that would allow me to add or change a configuration parameter inside a configuration file (ini-like format).

Most Linux configuration files use the INI, C # and; as a comment and with option = value.

I'm basically looking for something that will take a file name, parameter and value, and this will change the configuration to fit that.

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

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

Expected Result: have ULIMIT="-n 4096"nginx inside the file. Obviously, if it already exists and has the same meaning, it should not do anything. If it exists, commenting on the old line will be fine and adding a new one.

As a side note, these configuration files may have spaces / tabs between options, so if you have ULIMIT = "..."one, the same command is still there. That's why I was looking for something better than sed, since there are many angular cases to evaluate.

In addition, I do not want to reinvent the wheel, and I doubt that I am the first to seek a solution to this problem.

+4
source share
3 answers

git config - INI.

❱ 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"

. INI, , "somegroup". . , , , .

+8

Augeas/ augtool , , ( ), , Nginx.

API, .

+2

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

crudini --set /etc/default/nginx '' ULIMIT '"-n 4096"'
+2
source

Source: https://habr.com/ru/post/1534803/


All Articles