crudini is a utility to simplify reading and updating ini files from shell scripts, so named as it provides CRUD functionality.

Many programs in UNIX now use ini files for configuration, like systemd, yum, OpenStack and mysql, ..., and I've needed to programmatically manipulate them many times and was surprised that such a tool didn't already exist. There is augeas, though it's awkward to use. Also git config -f inifile is an option, though also a little awkward, quite git specific, and bundled in a 13MB git-core package on my distro.

crudini is robust in its file update implementation, applying ACID prinicples as detailed in my robust file replacement discussion.


INI formats supported

In general ini files are quite varied and non standard, though the format supported by crudini should handle the vast majority of situations. The best way to see the details of the supported ini format, is to look at the crudini example.ini used both for documentation and testing.

Interface synopsis

crudini --set [--existing] config_file section [param] [value]
        --get [--format=sh|ini|lines] config_file [section] [param]
        --del [--existing] config_file section [param] [list value]
        --merge [--existing] config_file [section]


Let's expand a bit on the last example with --format=lines which can be used to format full ini files so that they can be easily processed by the various UNIX test processing tools. Consider trying to diff two ini files, which have unordered sections. You can't directly sort the ini files, but we can transform them into a form that easily sortable, and thus consumable by various diff tools, uniq, join etc.
idiff <(crudini --get --format=lines fedora1.repo|sort) \
      <(crudini --get --format=lines fedora2.repo|sort)

--- /dev/fd/63	2013-05-16 01:11:22.012934833 +0100
+++ /dev/fd/62	2013-05-16 01:11:22.012934833 +0100
@@ -9,12 +9,11 @@µ
 [ fedora ] failovermethod = priority
 [ fedora ] gpgcheck = 1
 [ fedora ] gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch
-[ fedora ] metadata_expire = 7d
 [ fedora ] name = Fedora $releasever - $basearch
 [ fedora-source ] enabled = 0
 [ fedora-source ] failovermethod = priority
-[ fedora-source ] gpgcheck = 1
+[ fedora-source ] gpgcheck = 0
 [ fedora-source ] gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch
 [ fedora-source ] metadata_expire = 7d
For example, one can improve the git diff experience using the pattern above.
© May 16 2013