如何在shell脚本中获取INI值?

时间:2011-06-11 22:07:27

标签: bash shell ubuntu

我有一个parameters.ini文件,例如:

[parameters.ini]
    database_user    = user
    database_version = 20110611142248

我想读入并使用bash shell脚本中的parameters.ini文件中指定的数据库版本,以便我可以处理它。

#!/bin/sh    
# Need to get database version from parameters.ini file to use in script    
php app/console doctrine:migrations:migrate $DATABASE_VERSION

我该怎么做?

29 个答案:

答案 0 :(得分:70)

如何使用awk

为该行进行grepping
version=$(awk -F "=" '/database_version/ {print $2}' parameters.ini)

答案 1 :(得分:34)

您可以使用bash native解析器来解释ini值:

$ source <(grep = file.ini)

示例文件:

[section-a]
  var1=value1
  var2=value2
  IPS=( "1.2.3.4" "1.2.3.5" )

要访问变量,只需打印它们:echo $var1。您也可以使用如上所示的数组(echo ${IPS[@]})。

如果你只想要一个值grep:

source <(grep var1 file.ini)

这很简单,因为您不需要任何外部库来解析数据,但它有一些缺点。例如:

  • 如果=(变量名和值)之间有空格,那么您首先要修剪空格,例如

    $ source <(grep = file.ini | sed 's/ *= */=/g')
    
  • 要支持;条评论,请将其替换为#

    $ sed "s/;/#/g" foo.ini | source /dev/stdin
    
  • 不支持这些部分(例如,如果您[section-name],那么您必须如上所示过滤掉它,例如grep =),其他意外错误也是如此

    如果您需要阅读特定部分下的特定值,请使用grep -Asedawkex)。

    E.g。

    source <(grep = <(grep -A5 '\[section-b\]' file.ini))
    

    注意:-A5是要在该部分中读取的行数。将source替换为cat以进行调试。

  • 如果您有任何解析错误,请添加以下内容来忽略它们:2>/dev/null

另见:serverfault SE的How to parse and convert ini file into bash array variables?

答案 2 :(得分:27)

Bash不为这些文件提供解析器。显然你可以使用awk命令或几个sed调用,但如果你是bash-priest并且不想使用任何其他shell,那么你可以尝试以下模糊的代码:

#!/usr/bin/env bash
cfg_parser ()
{
    ini="$(<$1)"                # read the file
    ini="${ini//[/\[}"          # escape [
    ini="${ini//]/\]}"          # escape ]
    IFS=$'\n' && ini=( ${ini} ) # convert to line-array
    ini=( ${ini[*]//;*/} )      # remove comments with ;
    ini=( ${ini[*]/\    =/=} )  # remove tabs before =
    ini=( ${ini[*]/=\   /=} )   # remove tabs after =
    ini=( ${ini[*]/\ =\ /=} )   # remove anything with a space around =
    ini=( ${ini[*]/#\\[/\}$'\n'cfg.section.} ) # set section prefix
    ini=( ${ini[*]/%\\]/ \(} )    # convert text2function (1)
    ini=( ${ini[*]/=/=\( } )    # convert item to array
    ini=( ${ini[*]/%/ \)} )     # close array parenthesis
    ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick
    ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
    ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
    ini[0]="" # remove first element
    ini[${#ini[*]} + 1]='}'    # add the last brace
    eval "$(echo "${ini[*]}")" # eval the result
}

cfg_writer ()
{
    IFS=' '$'\n'
    fun="$(declare -F)"
    fun="${fun//declare -f/}"
    for f in $fun; do
        [ "${f#cfg.section}" == "${f}" ] && continue
        item="$(declare -f ${f})"
        item="${item##*\{}"
        item="${item%\}}"
        item="${item//=*;/}"
        vars="${item//=*/}"
        eval $f
        echo "[${f#cfg.section.}]"
        for var in $vars; do
            echo $var=\"${!var}\"
        done
    done
}

<强>用法:

# parse the config file called 'myfile.ini', with the following
# contents::
#   [sec2]
#   var2='something'
cfg.parser 'myfile.ini'

# enable section called 'sec2' (in the file [sec2]) for reading
cfg.section.sec2

# read the content of the variable called 'var2' (in the file
# var2=XXX). If your var2 is an array, then you can use
# ${var[index]}
echo "$var2"

可以在The Old School DevOps blog site找到Bash ini-parser。

答案 3 :(得分:17)

只需将.ini文件包含在bash正文中:

文件 example.ini

DBNAME=test
DBUSER=scott
DBPASSWORD=tiger

档案 example.sh

#!/bin/bash
#Including .ini file
. example.ini
#Test
echo "${DBNAME}   ${DBUSER}  ${DBPASSWORD}"

答案 4 :(得分:14)

Sed one-liner,考虑了部分。示例文件:

keyy

假设你想要第2节中的param2。运行以下命令:

[section1]
param1=123
param2=345
param3=678

[section2]
param1=abc
param2=def
param3=ghi

[section3]
param1=000
param2=111
param3=222

会给你

sed -nr "/^\[section2\]/ { :l /^param2[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" ./file.ini

答案 5 :(得分:11)

到目前为止,我见过的所有解决方案也都在注释掉了。如果评论代码是;,那么这个没有:

awk -F '=' '{if (! ($0 ~ /^;/) && $0 ~ /database_version/) print $2}' file.ini

答案 6 :(得分:11)

一种可能的解决方案

dbver=$(sed -n 's/.*database_version *= *\([^ ]*.*\)/\1/p' < parameters.ini)
echo $dbver

答案 7 :(得分:8)

在ini风格的 my_file 中显示 my_key 的值:

sed -n -e 's/^\s*my_key\s*=\s*//p' my_file
  • -n - 默认情况下不打印任何内容
  • -e - 执行表达式
  • s/PATTERN//p - 显示此模式后的任何内容 在模式中:
  • ^ - 模式从行的开头
  • 开始
  • \s - 空白字符
  • * - 零或多个(空格字符)

示例:

$ cat my_file
# Example INI file
something   = foo
my_key      = bar
not_my_key  = baz
my_key_2    = bing

$ sed -n -e 's/^\s*my_key\s*=\s*//p' my_file
bar

所以:

  

找到一行以零或多个空白字符开头的模式,   后跟字符串 my_key ,后跟零个或多个空白字符,等号,然后再返回零个或多个空白字符。在该模式之后显示该行上的其余内容。

答案 8 :(得分:4)

对于那些希望从shell脚本中读取INI文件的人(比如我)(阅读shell,而不是bash) - 我已经找到了一个小助手库,它试图做到这一点:

https://github.com/wallyhall/shini(麻省理工学院的许可证,请随意使用。我上面已经将其链接,因为代码非常冗长。)

它比上面提到的简单sed行更“复杂” - 但是在非常相似的基础上工作。

函数逐行读取文件 - 查找节标记([section])和键/值声明(key=value)。

最终你会得到一个回调你自己的功能 - 部分,键和值。

答案 9 :(得分:3)

您可以使用crudini工具获取ini值,例如:

DATABASE_VERSION=$(crudini --get parameters.ini '' database_version)

答案 10 :(得分:2)

您可以使用sed来解析ini配置文件,尤其是当您使用以下部分名称时:

# last modified 1 April 2001 by John Doe
[owner]
name=John Doe
organization=Acme Widgets Inc.

[database]
# use IP address in case network name resolution is not working
server=192.0.2.62
port=143
file=payroll.dat

因此您可以使用以下sed脚本来解析上述数据:

# Configuration bindings found outside any section are given to
# to the default section.
1 {
  x
  s/^/default/
  x
}

# Lines starting with a #-character are comments.
/#/n

# Sections are unpacked and stored in the hold space.
/\[/ {
  s/\[\(.*\)\]/\1/
  x
  b
}

# Bindings are unpacked and decorated with the section
# they belong to, before being printed.
/=/ {
  s/^[[:space:]]*//
  s/[[:space:]]*=[[:space:]]*/|/
  G
  s/\(.*\)\n\(.*\)/\2|\1/
  p
}

这会将ini数据转换为这种平面格式:

owner|name|John Doe
owner|organization|Acme Widgets Inc.
database|server|192.0.2.62
database|port|143
database|file|payroll.dat

因此,通过在每一行中包含部分名称,使用sedawkread进行解析会更容易。

Credits&amp;来源:Configuration files for shell scripts,MichaelGrünewald

答案 11 :(得分:2)

这是我的版本,它解析部分并用它填充全局关联数组 g_iniProperties 。 请注意,这仅适用于 bash v4.2及更高版本。

function parseIniFile() { #accepts the name of the file to parse as argument ($1)
    #declare syntax below (-gA) only works with bash 4.2 and higher
    unset g_iniProperties
    declare -gA g_iniProperties
    currentSection=""
    while read -r line
    do
        if [[ $line = [*  ]] ; then
            if [[ $line = [* ]] ; then 
                currentSection=$(echo $line | sed -e 's/\r//g' | tr -d "[]")  
            fi
        else
            if [[ $line = *=*  ]] ; then
                cleanLine=$(echo $line | sed -e 's/\r//g')
                key=$currentSection.$(echo $cleanLine | awk -F: '{ st = index($0,"=");print  substr($0,0,st-1)}')
                value=$(echo $cleanLine | awk -F: '{ st = index($0,"=");print  substr($0,st+1)}')
                g_iniProperties[$key]=$value
            fi
        fi;
    done < $1
}

以下是使用上述功能的示例代码:

parseIniFile "/path/to/myFile.ini"
for key in "${!g_iniProperties[@]}"; do
    echo "Found key/value $key = ${g_iniProperties[$key]}"
done

答案 12 :(得分:2)

有些答案不尊重评论。有些人不尊重他们。有些只识别一种语法(仅#34;:#34;或仅#34; =&#34;)。由于不同的captialization或无法导入sys模块,我的机器上的某些Python答案失败。对我来说,一切都太简洁了。

所以我写了自己的,如果你有一个现代的Python,你可以从你的Bash shell中调用它。它的优点是可以遵循一些常见的Python编码约定,甚至可以提供合理的错误消息和帮助。要使用它,请将其命名为myconfig.py(不要将其称为configparser.py,或者它可能尝试导入自身),使其可执行,并将其称为

value=$(myconfig.py something.ini sectionname value)

这是我在Linux上使用Python 3.5的代码:

#!/usr/bin/env python3
# Last Modified: Thu Aug  3 13:58:50 PDT 2017
"""A program that Bash can call to parse an .ini file"""

import sys
import configparser
import argparse

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="A program that Bash can call to parse an .ini file")
    parser.add_argument("inifile", help="name of the .ini file")
    parser.add_argument("section", help="name of the section in the .ini file")
    parser.add_argument("itemname", help="name of the desired value")
    args = parser.parse_args()

    config = configparser.ConfigParser()
    config.read(args.inifile)
    print(config.get(args.section, args.itemname))

答案 13 :(得分:2)

与其他Python答案类似,您可以使用-c标志执行此操作,以执行命令行中给出的一系列Python语句:

$ python3 -c "import configparser; c = configparser.ConfigParser(); c.read('parameters.ini'); print(c['parameters.ini']['database_version'])"
20110611142248

这样做的好处是只需要Python标准库,而且不需要编写单独的脚本文件。

或者使用here here以获得更好的可读性,因此:

#!/bin/bash
python << EOI
import configparser
c = configparser.ConfigParser()
c.read('params.txt')
print c['chassis']['serialNumber']
EOI

serialNumber=$(python << EOI
import configparser
c = configparser.ConfigParser()
c.read('params.txt')
print c['chassis']['serialNumber']
EOI
)

echo $serialNumber

答案 14 :(得分:1)

此脚本将获取如下参数:

意思是如果你的ini有:

  

pars_ini.ksh&lt;

例如。怎么称呼它:


[环境]

  

A = X

[DataBase_Sector]

  

DSN =某事


然后致电:

  

pars_ini.ksh /users/bubu_user/parameters.ini DataBase_Sector DSN

这将检索以下“某事”

脚本“pars_ini.ksh”:

\#!/bin/ksh

\#INI_FILE=path/to/file.ini

\#INI_SECTION=TheSection

\# BEGIN parse-ini-file.sh

\# SET UP THE MINIMUM VARS FIRST

alias sed=/usr/local/bin/sed

INI_FILE=$1

INI_SECTION=$2

INI_NAME=$3

INI_VALUE=""


eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \

    -e 's/;.*$//' \

    -e 's/[[:space:]]*$//' \

    -e 's/^[[:space:]]*//' \

    -e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \

   < $INI_FILE  \

    | sed -n -e "/^\[$INI_SECTION\]/,/^\s*\[/{/^[^;].*\=.*/p;}"`


TEMP_VALUE=`echo "$"$INI_NAME`

echo `eval echo $TEMP_VALUE`

答案 15 :(得分:0)

如果Python可用,则以下内容将读取所有节,键和值,并将它们保存在变量中,其名称遵循“ [section] _ [key]”格式。 Python可以正确读取.ini文件,因此我们可以使用它。

#!/bin/bash

eval $(python3 << EOP
from configparser import SafeConfigParser

config = SafeConfigParser()
config.read("config.ini"))

for section in config.sections():
    for (key, val) in config.items(section):
        print(section + "_" + key + "=\"" + val + "\"")
EOP
)

echo "Environment_type:  ${Environment_type}"
echo "Environment_name:  ${Environment_name}"

config.ini

[Environment]
  type                = DEV
  name                = D01

答案 16 :(得分:0)

使用 awk 的另一种实现方式具有更大的灵活性。

function parse_ini() {
  cat /dev/stdin | awk -v section="$1" -v key="$2" '
    BEGIN {
      if (length(key) > 0) { params=2 }
      else if (length(section) > 0) { params=1 }
      else { params=0 }
    }
    match($0,/#/) { next }
    match($0,/^\[(.+)\]$/){
      current=substr($0, RSTART+1, RLENGTH-2)
      found=current==section
      if (params==0) { print current }
    }
    match($0,/(.+)=(.+)/) {
       if (found) {
         if (params==2 && key==$1) { print $3 }
         if (params==1) { printf "%s=%s\n",$1,$3 }
       }
    }'
}

您可以使用在 0 到 2 个参数之间传递的调用:

cat myfile1.ini myfile2.ini | parse_ini # List section names

cat myfile1.ini myfile2.ini | parse_ini 'my-section' # Prints keys and values from a section

cat myfile1.ini myfile2.ini | parse_ini 'my-section' 'my-key' # Print a single value

答案 17 :(得分:0)

您可以使用CSV解析器xsv来解析INI数据。

cargo install xsv
$ cat /etc/*release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
$ xsv select -d "=" - <<< "$( cat /etc/*release )" | xsv search --no-headers --select 1 "DISTRIB_CODENAME" | xsv select 2
xenial

或来自文件。

$ xsv select -d "=" - file.ini | xsv search --no-headers --select 1 "DISTRIB_CODENAME" | xsv select 2

答案 18 :(得分:0)

如果使用部分,则可以完成工作:

原始输出示例:

$ ./settings
[section]
SETTING_ONE=this is setting one
SETTING_TWO=This is the second setting
ANOTHER_SETTING=This is another setting

正则表达式解析:

$ ./settings | sed -n -E "/^\[.*\]/{s/\[(.*)\]/\1/;h;n;};/^[a-zA-Z]/{s/#.*//;G;s/([^ ]*) *= *(.*)\n(.*)/\3_\1='\2'/;p;}"
section_SETTING_ONE='this is setting one'
section_SETTING_TWO='This is the second setting'
section_ANOTHER_SETTING='This is another setting'

现在在一起:

$ eval "$(./settings | sed -n -E "/^\[.*\]/{s/\[(.*)\]/\1/;h;n;};/^[a-zA-Z]/{s/#.*//;G;s/([^ ]*) *= *(.*)\n(.*)/\3_\1='\2'/;p;}")"
$ echo $section_SETTING_TWO
This is the second setting

答案 19 :(得分:0)

我有一线好(假设您已安装phpjq

cat file.ini | php -r "echo json_encode(parse_ini_string(file_get_contents('php://stdin'), true, INI_SCANNER_RAW));" | jq '.section.key'

答案 20 :(得分:0)

单线sed答案的解释。

[section1]
param1=123
param2=345
param3=678

[section2]
param1=abc
param2=def
param3=ghi

[section3]
param1=000
param2=111
param3=222
sed -nr "/^\[section2\]/ { :l /^\s*[^#].*/ p; n; /^\[/ q; b l; }" ./file.ini

要理解,格式化这样的行会更容易:

sed -nr "
      # start processing when we found the word \"section2\"
      /^\[section2\]/  { #the set of commands inside { } will be executed
          #create a label \"l\"  (https://www.grymoire.com/Unix/Sed.html#uh-58)
          :l /^\s*[^#].*/ p; 
          # move on to the next line. For the first run it the \"param1=abc\"
          n; 
          # check if this line is beginning of new section. If yes - then exit.
          /^\[/ q
          #otherwise jump to the label \"l\"
          b l
          }

" file.ini

答案 21 :(得分:0)

此线程没有足够的解决方案可供选择,因此在这里,我的解决方案不需要sedawk之类的工具:

grep '^\[section\]' -A 999 config.ini | tail -n +2  | grep -B 999 '^\[' | head -n -1 | grep '^key' | cut -d '=' -f 2 

如果您希望包含999行以上的部分,请随时调整上面的示例。请注意,您可能需要修剪结果值,以删除该值后的空格或注释字符串。如果您需要匹配不在行开头的键,请删除^,如问题示例所示。更好的是,在这种情况下,显式匹配空格和制表符。

如果给定部分中有多个值,则要读取,但要避免多次读取文件:

CONFIG_SECTION=$(grep '^\[section\]' -A 999 config.ini | tail -n +2  | grep -B 999 '^\[' | head -n -1)

KEY1=$(echo ${CONFIG_SECTION} | tr ' ' '\n' | grep key1 | cut -d '=' -f 2)
echo "KEY1=${KEY1}"
KEY2=$(echo ${CONFIG_SECTION} | tr ' ' '\n' | grep key2 | cut -d '=' -f 2)
echo "KEY2=${KEY2}"

答案 22 :(得分:0)

“卡伦·加布里埃尔(Karen Gabrielyan)”的答案是最好的,但是在某些情况下我们没有awk,例如典型的busybox,我通过以下代码更改了答案。

trim()
{
    local trimmed="$1"

    # Strip leading space.
    trimmed="${trimmed## }"
    # Strip trailing space.
    trimmed="${trimmed%% }"

    echo "$trimmed"
}


  function parseIniFile() { #accepts the name of the file to parse as argument ($1)
        #declare syntax below (-gA) only works with bash 4.2 and higher
        unset g_iniProperties
        declare -gA g_iniProperties
        currentSection=""
        while read -r line
        do
            if [[ $line = [*  ]] ; then
                if [[ $line = [* ]] ; then 
                    currentSection=$(echo $line | sed -e 's/\r//g' | tr -d "[]")  
                fi
            else
                if [[ $line = *=*  ]] ; then
                    cleanLine=$(echo $line | sed -e 's/\r//g')
                    key=$(trim $currentSection.$(echo $cleanLine | cut -d'=' -f1'))
                    value=$(trim $(echo $cleanLine | cut -d'=' -f2))
                    g_iniProperties[$key]=$value
                fi
            fi;
        done < $1
    }

答案 23 :(得分:0)

这使用系统perl并清除正则表达式:

  cat parameters.ini | perl -0777ne'如果/\[\s*parameters\.ini\s*\][\s\S]*?\sdatabase_version\s*=\s*(.*)/',则打印“ $ 1”
 

答案 24 :(得分:0)

复杂简单

ini文件

test.ini

[section1]
name1=value1
name2=value2
[section2]
name1=value_1
  name2  =  value_2

带有读取和执行的bash脚本

/ bin中/ parseini

#!/bin/bash

set +a
while read p; do
  reSec='^\[(.*)\]$'
  #reNV='[ ]*([^ ]*)+[ ]*=(.*)'     #Remove only spaces around name
  reNV='[ ]*([^ ]*)+[ ]*=[ ]*(.*)'  #Remove spaces around name and spaces before value
  if [[ $p =~ $reSec ]]; then
      section=${BASH_REMATCH[1]}
  elif [[ $p =~ $reNV ]]; then
    sNm=${section}_${BASH_REMATCH[1]}
    sVa=${BASH_REMATCH[2]}
    set -a
    eval "$(echo "$sNm"=\""$sVa"\")"
    set +a
  fi
done < $1

然后在另一个脚本中我获取命令的结果并可以使用

中的任何变量

test.sh

#!/bin/bash

source parseini test.ini

echo $section2_name2

最后从命令行输出

# ./test.sh 
value_2

答案 25 :(得分:0)

我写了一个快速简单的python脚本,包含在我的bash脚本中。

例如,您的ini文件名为food.ini 在文件中你可以有一些部分和一些行:

[FRUIT]
Oranges = 14
Apples = 6

复制这个小的6行Python脚本并将其保存为configparser.py

#!/usr/bin/python
import configparser
import sys
config = configpParser.ConfigParser()
config.read(sys.argv[1])
print config.get(sys.argv[2],sys.argv[3])

现在,在您的bash脚本中,您可以执行此操作。

OrangeQty=$(python configparser.py food.ini FRUIT Oranges)

ApplesQty=$(python configparser.py food.ini FRUIT Apples)
echo $ApplesQty

这个目的:

  1. 你安装了Python
  2. 你安装了configparser库(这应该带有std python安装)
  3. 希望它有所帮助 :¬)

答案 26 :(得分:0)

此实现使用awk并具有以下优点:

  1. 仅返回第一个匹配条目
  2. 忽略以;
  3. 开头的行
  4. 修剪前导和尾随空格,但不修剪内部空白
  5. 格式化版本

    awk -F '=' '/^\s*database_version\s*=/ {
                sub(/^ +/, "", $2);
                sub(/ +$/, "", $2);
                print $2;
                exit;
              }' parameters.ini
    

    <强>一衬垫

    awk -F '=' '/^\s*database_version\s*=/ { sub(/^ +/, "", $2); sub(/ +$/, "", $2); print $2; exit; }' parameters.ini
    

答案 27 :(得分:0)

刚写完我自己的解析器。我试图使用这里找到的各种解析器,似乎没有一个与ksh93(AIX)和bash(Linux)一起使用。

它的旧编程风格 - 逐行解析。很快,因为它使用了很少的外部命令。由于数组动态名称所需的所有eval,因此速度稍慢。

ini支持3种特殊语法:

  • includefile = ini file - &gt; 加载一个额外的ini文件。用于在多个文件中拆分ini,或重用某些配置
  • includedir = directory - &gt; 与includefile相同,但包含完整目录
  • includesection = section - &gt; 将现有部分复制到当前部分。

我使用所有thoses语法来拥有相当复杂,可重用的ini文件。在安装新操作系统时安装产品很有用 - 我们做了很多。

可以使用$ {ini [$ section。$ item]}访问值。必须在调用之前定义数组。

玩得开心。希望它对其他人有用!

function Show_Debug {
    [[ $DEBUG = YES ]] && echo "DEBUG $@"
    }

function Fatal {
    echo "$@. Script aborted"
    exit 2
    }
#-------------------------------------------------------------------------------
# This function load an ini file in the array "ini"
# The "ini" array must be defined in the calling program (typeset -A ini)
#
# It could be any array name, the default array name is "ini".
#
# There is heavy usage of "eval" since ksh and bash do not support
# reference variable. The name of the ini is passed as variable, and must
# be "eval" at run-time to work. Very specific syntax was used and must be
# understood before making any modifications.
#
# It complexify greatly the program, but add flexibility.
#-------------------------------------------------------------------------------

function Load_Ini {
    Show_Debug "$0($@)"
    typeset ini_file="$1"
# Name of the array to fill. By default, it's "ini"
    typeset ini_array_name="${2:-ini}"
    typeset section variable value line my_section file subsection value_array include_directory all_index index sections pre_parse
    typeset LF="
"
    if [[ ! -s $ini_file ]]; then
        Fatal "The ini file is empty or absent in $0 [$ini_file]"
    fi

    include_directory=$(dirname $ini_file)
    include_directory=${include_directory:-$(pwd)}

    Show_Debug "include_directory=$include_directory"

    section=""
# Since this code support both bash and ksh93, you cannot use
# the syntax "echo xyz|while read line". bash doesn't work like
# that.
# It forces the use of "<<<", introduced in bash and ksh93.

    Show_Debug "Reading file $ini_file and putting the results in array $ini_array_name"
    pre_parse="$(sed 's/^ *//g;s/#.*//g;s/ *$//g' <$ini_file | egrep -v '^$')"
    while read line; do
        if [[ ${line:0:1} = "[" ]]; then # Is the line starting with "["?
# Replace [section_name] to section_name by removing the first and last character
            section="${line:1}"
            section="${section%\]}"
            eval "sections=\${$ini_array_name[sections_list]}"
            sections="$sections${sections:+ }$section"
            eval "$ini_array_name[sections_list]=\"$sections\""
            Show_Debug "$ini_array_name[sections_list]=\"$sections\""
            eval "$ini_array_name[$section.exist]=YES"
            Show_Debug "$ini_array_name[$section.exist]='YES'"
        else
            variable=${line%%=*}   # content before the =
            value=${line#*=}       # content after the =

            if [[ $variable = includefile ]]; then
# Include a single file
                Load_Ini "$include_directory/$value" "$ini_array_name"
                continue
            elif [[ $variable = includedir ]]; then
# Include a directory
# If the value doesn't start with a /, add the calculated include_directory
                if [[ $value != /* ]]; then
                    value="$include_directory/$value"
                fi
# go thru each file
                for file in $(ls $value/*.ini 2>/dev/null); do
                    if [[ $file != *.ini ]]; then continue; fi
# Load a single file
                    Load_Ini "$file" "$ini_array_name"
                done
                continue
            elif [[ $variable = includesection ]]; then
# Copy an existing section into the current section
                eval "all_index=\"\${!$ini_array_name[@]}\""
# It's not necessarily fast. Need to go thru all the array
                for index in $all_index; do
# Only if it is the requested section
                    if [[ $index = $value.* ]]; then
# Evaluate the subsection [section.subsection] --> subsection
                        subsection=${index#*.}
# Get the current value (source section)
                        eval "value_array=\"\${$ini_array_name[$index]}\""
# Assign the value to the current section
# The $value_array must be resolved on the second pass of the eval, so make sure the
# first pass doesn't resolve it (\$value_array instead of $value_array).
# It must be evaluated on the second pass in case there is special character like $1,
# or ' or " in it (code).
                        eval "$ini_array_name[$section.$subsection]=\"\$value_array\""
                        Show_Debug "$ini_array_name[$section.$subsection]=\"$value_array\""
                    fi
                done
            fi

# Add the value to the array
            eval "current_value=\"\${$ini_array_name[$section.$variable]}\""
# If there's already something for this field, add it with the current
# content separated by a LF (line_feed)
            new_value="$current_value${current_value:+$LF}$value"
# Assign the content
# The $new_value must be resolved on the second pass of the eval, so make sure the
# first pass doesn't resolve it (\$new_value instead of $new_value).
# It must be evaluated on the second pass in case there is special character like $1,
# or ' or " in it (code).
            eval "$ini_array_name[$section.$variable]=\"\$new_value\""
            Show_Debug "$ini_array_name[$section.$variable]=\"$new_value\""
        fi
    done  <<< "$pre_parse"
    Show_Debug "exit $0($@)\n"
    }

答案 28 :(得分:0)

我的单线版

#!/bin/bash
#Reader for MS Windows 3.1 Ini-files
#Usage: inireader.sh

# e.g.: inireader.sh win.ini ERRORS DISABLE
# would return value "no" from the section of win.ini
#[ERRORS]
#DISABLE=no
INIFILE=$1
SECTION=$2
ITEM=$3
cat $INIFILE | sed -n /^\[$SECTION\]/,/^\[.*\]/p | grep "^[:space:]*$ITEM[:space:]*=" | sed s/.*=[:space:]*//