Ou alors, en restant vraiment en shell (bash 4.0+):
#------------------------------------------------------------------------------# Parses a .ini file## License:# GPLv2## Parse the specified .ini file, and stores the results in caller-supplied# variable arrays:## * +$sections[]+: indexed array# * +$variables[]+: associative array# * +$values[]+: associative array# * +$lines[]+: associative array## Those variables will be filled as thus:## sections=( [0]='section-0' [1]='section-1' )# variables=( ['section-0']='var-0,var-1' ['section-1']='var-10,var-11' [...] )# values=( ['section-0:var-0']='value-0-0' ['section-1:var-10']='value-1-10' [...] )# lines=( ['section-0']='file:27' ['section-1:var-10']='file:42' [...] )## Note that the accepted syntax is a subset of the .ini format, where only# '=' is accepted to assign variables, while ':' is not accepted.## Param:# 1ドル: .ini file to parse## Return:# $sections[] : the list of sections, one section per array index# $variables[]: the comma-separated list of variables for a section,# indexed by the name of the section# $values[] : the value of a variable in a section, indexed by the# 'section:variable' tuple# $lines[] : the 'file:line' a section or variable was defined on,# indexed by the 'section' name, or the 'section:variable'# tuple#
parse_ini(){local ini_file="${1}"local line var val i
local cur_section
i=0
whileread line;do
: $((i++))# Mangle the line:# - get rid of comments# - get rid of spaces around the first '='line="$( sed -r -e 's/[[:space:]]*#.*$//; //d;' \ -e 's/=/ = /;' -e 's/[[:space:]]+=[[:space:]]+/=/' \ <<<"${line}" )"case"${line}" in
"")continue;;'['*']')cur_section="$( sed -r -e 's/[][]//g;' <<<"${line}" )"
sections+=("${cur_section}")
lines+=(["${cur_section}"]="${ini_file}:${i}")continue;;
?*=*);;
*)printf"malformed entry '%s' in '%s:%d'\n""${line}""${ini_file}"${i} >&2;return 1;;esacvar="${line%%=*}"eval val="${line#*=}"
variables+=(["${cur_section}"]=",${var}")
values+=(["${cur_section}:${var}"]="${val}")
lines+=(["${cur_section}:${var}"]="${ini_file}:${i}")done <"${ini_file}"}
# Vraiment en shell
Posté par ymorin . En réponse au journal Lire de fichiers de configuration depuis un script shell. Évalué à 4.
Ou alors, en restant vraiment en shell (bash 4.0+):
Hop,
Moi.