2

I'm building a custom package of vnc and would like to ensure the xdcmp settings of GDM are enabled in the package post install script. The gdm.conf file is an ini style one, i.e.:

[section]
var=name

And the value I want to set has name clashes in different sections throughout the config file.

Are there any methods or tools that allow for easy manipulation of ini style config files from shell scripts?

I'd like to sort this out in the .deb postinst script.

kenorb
  • 155,785
  • 88
  • 678
  • 743
stsquad
  • 5,712
  • 3
  • 36
  • 54

3 Answers3

2

Have a look at the crudini package. It's designed for manipulating ini files from shell

pixelbeat
  • 30,615
  • 9
  • 51
  • 60
1

If you're willing to write some Perl, there's Config::IniFiles (package libconfig-inifiles-perl).

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
  • I'm coming to conclusion that perl is the easiest solution. The Debian packaging guidelines don't seem to disallow perl for configure scripts although I suspect adding a dependency on perl just to install a package will mean this will never go upstream. – stsquad Aug 12 '10 at 10:46
  • 1
    @stsquad: Perl itself is not a problem, it's officially blessed for use in Debian package scripts (and you can see for example that `perl-base` is essential, and `debconf` provides Perl modules). Having to pre-depend on `libconfig-inifiles-perl` might be a bigger obstable. – Gilles 'SO- stop being evil' Aug 12 '10 at 11:41
0

Shell command using Ex editor (to change the value of var key):

ex +"%s/^var=\zs.*/new_name/" -scwq config.ini

To support INI sections, use the following syntax:

ex +':/\[section\]/,$s/var=\zs.*/new_name/' -scwq config.ini

For reading values from INI files, see: How do I grab an INI value within a shell script?


Here is the shell function which can be helpful for editing INI values (not supporting sections):

# Set value in the INI file.
# Usage: ini_set [key] [value] [file]
ini_set()
{
  local key="$1"
  local value="$2"
  local file="$3"
  [ -f "$file" ]
  if [ -n "$value" ]; then
    if grep -q "$key" "$file"; then
      echo "INFO: Setting '$key' to '$value' in $(basename "$file")"
      ex +'%s#'"$key"'=\zs.*$#'"$value"'#' -scwq! "$file"
    else
      echo "$key=$value" >> "$file"
    fi
  else
    echo "WARN: Value for '$key' is empty, ignoring."
  fi
}

Here is shell function to read INI values (not supporting sections):

# Get value from the INI file.
# Usage: ini_get [key] (file)
ini_get()
{
  local key="$1"
  local file="$2"
  [ ! -s "$file" ] && return
  local value="$(grep -om1 "^$key=\S\+" "$file" | head -1 | cut -d= -f2-)"
  echo "Getting '$key' from $(basename "$file"): $value" >&2
  echo $value
}

E.g. ini_get var.

kenorb
  • 155,785
  • 88
  • 678
  • 743