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

问题描述 投票:77回答:24

我有一个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

我该怎么做?

bash shell ubuntu
24个回答
72
投票

那么使用awk然后为该行进行grepping

version=$(awk -F "=" '/database_version/ {print $2}' parameters.ini)

3
投票

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

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

2
投票

有些答案不尊重评论。有些人不尊重部分。有些只识别一种语法(仅“:”或仅“=”)。由于不同的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))

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

2
投票

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

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

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

或者使用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

2
投票

sed

您可以使用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通过在每一行中包含节名称来更容易解析。

致谢与来源:Configuration files for shell scripts,MichaelGrünewald


或者,您可以使用此项目:chilladx/config-parser,一个使用sed的配置解析器。


1
投票

该脚本将获取如下参数:

意思是如果你的ini有:

pars_ini.ksh <ini文件的路径> <Ini文件中的扇区名称> <名称中的名称=要返回的值>

例如。怎么称呼它:


[ 环境 ]

一个= 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`

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:]*//

0
投票

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

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

迷你支持3种特殊语法:

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

我使用所有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"
    }

0
投票

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

  1. 只会返回第一个匹配的条目
  2. 忽略以;开头的行
  3. 修剪前导和尾随空格,但不修饰内部空白

格式化版本:

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

0
投票

当我在base64中使用密码时,我把分隔符“:”,因为base64字符串可能有“=”。例如(我使用ksh):

> echo "Abc123" | base64
QWJjMTIzCg==

parameters.ini放线pass:QWJjMTIzCg==,最后:

> PASS=`awk -F":" '/pass/ {print $2 }' parameters.ini | base64 --decode`
> echo "$PASS"
Abc123

如果该行有像"pass : QWJjMTIzCg== "这样的空格,请添加| tr -d ' '来修剪它们:

> PASS=`awk -F":" '/pass/ {print $2 }' parameters.ini | tr -d ' ' | base64 --decode`
> echo "[$PASS]"
[Abc123]

37
投票

您可以使用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') 或者,如果您不关心空间(包括中间),请使用: $ source <(grep = file.ini | tr -d ' ')
  • 要支持;评论,请将其替换为#$ sed "s/;/#/g" foo.ini | source /dev/stdin
  • 不支持这些部分(例如,如果你有[section-name],那么你必须如上所示过滤它,例如grep =),其他意外错误也是如此。 如果您需要阅读特定部分下的特定值,请使用grep -Asedawkex)。 例如。 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?


0
投票

简单复杂

这个文件

test.ini

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

带有读取和执行的bash脚本

/斌/ 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.是

#!/bin/bash

source parseini test.ini

echo $section2_name2

最终从命令行输出

# ./test.sh 
value_2

0
投票

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

cat parameters.ini | perl -0777ne 'print "$1" if /\[\s*parameters\.ini\s*\][\s\S]*?\sdatabase_version\s*=\s*(.*)/'

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
    }

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

0
投票

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

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

[FRUIT]
Oranges = 14
Apples = 6

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

#!/usr/bin/python
import configparser
import sys
config = configparser.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安装)

希望它有所帮助:¬)


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

28
投票

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。


19
投票

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

Example.ini文件:

DBNAME=test
DBUSER=scott
DBPASSWORD=tiger

文件example.sh

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

16
投票

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

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

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

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

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

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

会给你

def

11
投票

一种更可能的解决方案

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

11
投票

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

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

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,后跟零或许多空白字符,等号,然后再返回零或许多空白字符。在该模式之后显示该行上的其余内容。


4
投票

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

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.