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

| 我有一个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
我该怎么做?     
已邀请:
        如何使用awk对该行进行grepping
version=$(awk -F \"=\" \'/database_version/ {print $2}\' parameters.ini)
    
        您可以通过以下方式使用bash本机解析器来解释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 -A
sed
awk
ex
)。 例如。
source <(grep = <(grep -A5 \'\\[section-b\\]\' file.ini))
注意:其中“ 21”是该部分中要读取的行数。将
source
替换为
cat
进行调试。 如果您有任何解析错误,请添加以下内容来忽略它们:
2>/dev/null
另请参阅:如何解析ini文件并将其转换为bash数组变量?在serverfault SE     
        Bash不为这些文件提供解析器。显然,您可以使用awk命令或几个sed调用,但是如果您是bash牧师并且不想使用任何其他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\"
Bash ini-parser可以在The Old School DevOps博客网站上找到。     
        sed单线,考虑了部分。示例文件:
[section1]
param1=123
param2=345
param3=678

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

[section3]
param1=000
param2=111
param3=222
假设您要从section2获得param2。运行以下命令:
sed -nr \"/^\\[section2\\]/ { :l /^param2[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}\" ./file.ini
会给你
def
    
        只需将您的.ini文件包含在bash主体中: 文件example.ini:
DBNAME=test
DBUSER=scott
DBPASSWORD=tiger
文件example.sh
#!/bin/bash
#Including .ini file
. example.ini
#Test
echo \"${DBNAME}   ${DBUSER}  ${DBPASSWORD}\"
    
        到目前为止,我所看到的所有解决方案也都在注释行中出现。如果注释代码为
;
,则此代码没有:
awk -F \'=\' \'{if (! ($0 ~ /^;/) && $0 ~ /database_version/) print $2}\' file.ini
    
        一种可能的解决方案之一
dbver=$(sed -n \'s/.*database_version *= *\\([^ ]*.*\\)/\\1/p\' < parameters.ini)
echo $dbver
    
        以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,后跟零个或多个空格字符,等号,然后再次零个或多个空格字符。按照该模式在该行上显示其余内容。     
        对于希望从shell脚本中读取INI文件的人(像我一样)(读取shell,而不是bash)-我已经打开了一个小助手库,它试图做到这一点: https://github.com/wallyhall/shini(MIT许可证,请根据需要进行操作。由于代码很长,我已经在上面进行了内联,包括它在内)。 它比上面建议的简单的
sed
行稍微“复杂”,但工作原理非常相似。 函数逐行读取文件-查找节标记(
[section]
)和键/值声明(
key=value
)。 最终,您将获得对自己的函数的回调-部分,键和值。     
        您可以使用
crudini
工具获取ini值,例如:
DATABASE_VERSION=$(crudini --get parameters.ini \'\' database_version)
    
        sed 您可以使用ѭ17来解析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
因此您可以使用以下“ 17”脚本来解析上述数据:
# 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
因此使用section17 having,
awk
read
更容易解析,因为每行都有节名称。 鸣谢与出处:Shell脚本的配置文件,MichaelGrünewald 或者,您可以使用以下项目:
chilladx/config-parser
,使用
sed
的配置解析器。     
有些答案不尊重评论。有些人不尊重章节。有些仅识别一种语法(仅\“:\”或仅\“ = \”)。由于大小写不同或无法导入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))
    
        这是我的版本,它解析节并用它填充全局关联数组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
    
        与其他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
    
        该脚本将获取如下参数: 表示如果您的ini具有:   pars_ini.ksh <名称中的名称=要返回的值> 例如。怎么称呼它: [ 环境 ]   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`
    
        复杂的简单 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
    
        我编写了一个快速简单的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
前提是: 您已经安装了Python 您已经安装了configparser库(应该随std python安装一起提供) 希望能帮助到你 :¬)     
        我的单线版
#!/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:]*//
    
        刚写完我自己的解析器。我试图使用这里找到的各种解析器,似乎没有一个同时适用于ksh93(AIX)和bash(Linux)。 它是古老的编程风格-逐行解析。由于它使用了很少的外部命令,因此速度非常快。由于阵列动态名称需要所有评估,因此速度稍慢。 ini支持3种特殊语法: includefile = ini文件-> 加载一个附加的ini文件。将ini拆分为多个文件或重新使用某些配置很有用 includedir =目录-> 与includefile相同,但包含完整目录 includesection = section-> 将现有节复制到当前节。 我使用所有这些语法来制作非常复杂,可重复使用的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\"
    }
    
        此实现使用ѭ18并具有以下优点: 只返回第一个匹配项 忽略以ѭ11开头的行 修剪前导和尾随空白,但不修剪内部空白 格式化版本:
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
    
        当我在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]
    
        这使用系统perl并清除正则表达式:
cat parameters.ini | perl -0777ne \'print \"$1\" if /\\[\\s*parameters\\.ini\\s*\\][\\s\\S]*?\\sdatabase_version\\s*=\\s*(.*)/\'
    
        “ 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
    }
    
如果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
    
        您可以使用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
    

要回复问题请先登录注册