Created
January 15, 2014 14:53
-
-
Save arcadoss/8437681 to your computer and use it in GitHub Desktop.
ini config parser for shell it bug when values or keys contains a ‘#’ sign see also http://ajdiaz.wordpress.com/2008/02/09/bash-ini-parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Author: CJ | |
# Date..: 01/03/2013 | |
## sample INI file save below to a file, replace "^I" with tab | |
#^I [ SECTION ONE ] | |
#TOKEN_TWO^I ="Value1 two " | |
#TOKEN_ONE=Value1 One | |
#TOKEN_THREE=^I"Value1^I three" # a comment string | |
#TOKEN_FOUR^I=^I"^IValue1 four" | |
# | |
#[SECTION_TWO] | |
#TOKEN_ONE=Value1 One ^I^I^I# another comment string | |
#TOKEN_TWO^I ="Value1 two " | |
#TOKEN_THREE=^I"Value1^I three" | |
#TOKEN_FOUR^I=^I"^IValue1 four" | |
## sample INI file | |
export INI= # allows access to the parsed INI values in toto by children | |
iniParse() { | |
# Make word separator Linefeed(\n) | |
OIFS="${IFS}" | |
IFS=$(echo) | |
SECTION=_ | |
while read LINE; do { | |
IFS="${OIFS}" | |
# Skip blank lines | |
TMP="$(echo "${LINE}"|sed -e "s/^[ \t]*//")" | |
if [ 0 -ne ${#TMP} ]; then | |
# Ignore comment lines | |
if [ '#' == "${LINE:0:1}" -o '*' == "${LINE:0:1}" ]; then | |
continue | |
fi # if [ '#' == "${LINE:0:1}" -o '*' == "${LINE:0:1}" ]; then | |
# Section label | |
if [ "[" == "${LINE:0:1}" ]; then | |
LINE="${LINE/[/}" | |
LINE="${LINE/]/}" | |
LINE="${LINE/ /_}" | |
SECTION=$(echo "${LINE}")_ | |
else | |
LINE="$(echo "${LINE}"|sed -e "s/^[ \t]*//")" | |
LINE="$(echo "${LINE}"|cut -d# -f1)" | |
TOKEN="$(echo "${LINE:0}"|cut -d= -f1)" | |
EQOFS=${#TOKEN} | |
TOKEN="$(echo "${TOKEN}"|sed -e "s/[ \t]*//g")" | |
VALUE="${LINE:${EQOFS}}" | |
VALUE="$(echo "${VALUE}"|sed -e "s/^[ \t=]*//")" | |
VALUE="$(echo "${VALUE}"|sed -e "s/[ \t]*$//")" | |
if [ "${VALUE:0:1}" == '"' ]; then | |
echo -n "${SECTION}${TOKEN}=${VALUE}" | |
echo -e "\r" | |
else | |
echo -n "${SECTION}${TOKEN}="\"${VALUE}\""" | |
echo -e "\r" | |
fi # if [ "${VALUE:0:1}" == '"' ]; then | |
fi # if [ "[" == "${LINE:0:1}" ]; then | |
fi # if [ 0 -ne ${#TMP} ]; then | |
IFS=$(echo) | |
} done <<< "$1" | |
IFS="${OIFS}" # restore original IFS value | |
} # iniParse() | |
# call this function with the INI filespec | |
iniReader() { | |
if [ -z "$1" ]; then return 1; fi | |
TMPINI="$(<$1)" | |
TMPINI="$(echo "${TMPINI}"|sed -e "s/\r//g")" | |
TMPINI="$(echo "${TMPINI}"|sed -e "s/[ \t]*\[[ \t]*/[/g")" | |
TMPINI="$(echo "${TMPINI}"|sed -e "s/[ \t]*\][ \t]*/]/g")" | |
INI=`iniParse "${TMPINI}"` | |
INI="$(echo "${INI}"|sed -e "s/\r/\n/g")" | |
eval "${INI}" | |
return 0 | |
} # iniReader() { | |
# sample usage | |
if iniReader $1 ; then | |
echo INI read, exit_code $? # exit_code == 0 | |
cat <<< "${INI}" | |
cat <<< "${SECTION_ONE_TOKEN_FOUR}" | |
cat <<< "${SECTION_ONE_TOKEN_THREE}" | |
cat <<< "${SECTION_TWO_TOKEN_TWO}" | |
cat <<< "${SECTION_TWO_TOKEN_ONE}" | |
else | |
echo usage: $0 filename.ini | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment