如何解析ini文件并将其转换为bash数组变量?

Fli*_*int 17 scripting configuration bash awk sed

我正在尝试将 ini 文件转换为 bash 数组变量。示例ini如下:

[foobar]
session=foo
path=/some/path

[barfoo]
session=bar
path=/some/path
Run Code Online (Sandbox Code Playgroud)

所以这些变成:

session[foobar]=foo
path[foobar]=/some/path
session[barfoo]=bar
Run Code Online (Sandbox Code Playgroud)

等等。

现在,我只能想出这个命令

awk -F'=' '{ if ($1 ~ /^\[/) section=$1; else if ($1 !~ /^$/) print $1 section "=" $2 }'
Run Code Online (Sandbox Code Playgroud)

此外,另一个问题是,它没有考虑附近的空间=。我认为sed可能更适合这项工作,但我不知道如何在sed.

那么知道如何做到这一点吗?

Den*_*son 14

Gawk 接受正则表达式作为字段分隔符。下面消除了等号周围的空格,但将它们保留在行的其余部分。在值周围添加引号,以便在执行 Bash 分配时保留这些空格(如果有)。我假设部分名称将是数字变量,但是如果您使用 Bash 4,则很容易将其调整为使用关联数组,并将部分名称本身作为索引。

awk -F ' *= *' '{ if ($1 ~ /^\[/) section=$1; else if ($1 !~ /^$/) print $1 section "=" "\"" $2 "\"" }'
Run Code Online (Sandbox Code Playgroud)

请注意,由于 Bash 变量名称不能包含空格,因此您可能还想删除 Khaled 显示的空格(仅在 $1 和节上)。

此外,如果值包含等号,则此方法将不起作用。

另一种技术是使用 Bashwhile read循环并在读取文件时执行分配,使用declare它可以避免大多数恶意内容。

foobar=1
barfoo=2  # or you could increment an index variable each time a section is found
while IFS='= ' read var val
do
    if [[ $var == \[*] ]]
    then
        section=$var
    elif [[ $val ]]
    then
        declare "$var$section=$val"
    fi
done < filename
Run Code Online (Sandbox Code Playgroud)

同样,可以很容易地支持关联数组。


Mic*_*jer 13

我会使用简单的 python 脚本来完成这项工作,因为它内置了 INI解析器

#!/usr/bin/env python

import sys, ConfigParser

config = ConfigParser.ConfigParser()
config.readfp(sys.stdin)

for sec in config.sections():
    print "declare -A %s" % (sec)
    for key, val in config.items(sec):
        print '%s[%s]="%s"' % (sec, key, val)
Run Code Online (Sandbox Code Playgroud)

然后在 bash 中:

#!/bin/bash

# load the in.ini INI file to current BASH - quoted to preserve line breaks
eval "$(cat in.ini  | ./ini2arr.py)"

# test it:
echo ${barfoo[session]}
Run Code Online (Sandbox Code Playgroud)

当然,awk 中有更短的实现,但我认为这更具可读性且更易于维护。

  • 在 4.2 之前的 bash 版本中,需要在填充之前声明一个关联数组,例如 `print "declare -A %s" % (sec)` (3认同)
  • 代替 `eval`:`source &lt;(cat in.ini | ./ini2arr.py)` (2认同)