Skip to content

Instantly share code, notes, and snippets.

@saxenap
Last active August 29, 2015 14:15
Show Gist options
  • Save saxenap/fd1328c6e6cfa0b2b08a to your computer and use it in GitHub Desktop.
Save saxenap/fd1328c6e6cfa0b2b08a to your computer and use it in GitHub Desktop.
This script acts as an adapter to the Bash_INI_Parser.
#!/bin/bash
#
# Copyright (c) 2014-2017 Praveen Saxena <>
# License: BSD-3-Clause
#
# To Use:
# wget -O IniParserToParameterAdapter https://gist.githubusercontent.com/saxenap/fd1328c6e6cfa0b2b08a/raw
#
# Requires:
# 1. Common Functions from https://gist.githubusercontent.com/saxenap/073049d1ead105417837/raw
# 2. Ini Parser from https://github.com/rudimeier/bash_ini_parser
# Arg1: Common Functions
# Arg2: INI Parser
main()
{
local helpers="${1}"
local iniParser="${2}"
source ${helpers}
source ${iniParser}
}
if [ "${1}" != "--source-only" ]; then
main "${@}"
fi
readIniFile()
{
set +o nounset
set +o errexit
local filename=$@
read_ini "${filename}" && rc=$? || rc=$?
set -o errexit
set -o nounset
if [[ ${rc} != 0 ]] ; then
die "read_ini exited with error code ${rc}."
fi
}
# Call this function to parse a section.
# Stores parsed keys in a global array as INI__{SectionName}
# Example:
# parseVarsInSection "PhpPackagesToInstall"
# This would store keys from section "PhpPackagesToInstall"
# in global array INI__PhpPackagesToInstall
parseVarsInSection()
{
local section="${1}"
local i=0
eval "INI__${section}=()"
for (( ; ; ))
do
((++i))
local iniVarname="$(makeIniVariableFor ${section} ${i})"
local dummy=$(iniValueFor ${iniVarname})
[ -z "$dummy" ] && break
eval "INI__${section}[$i]=${dummy}"
done
}
# Returns the value or empty string for given ini variable
# Usage: iniValueFor INI__PHP__2
iniValueFor()
{
local iniVarname="${1}"
eval "local iniVarValue=\${$iniVarname:-}"
echo "${iniVarValue}"
}
makeIniVariableFor()
{
local section="${1}"
local key="${2}"
local var="INI__${section}__${key}"
echo "${var}"
}
@saxenap
Copy link
Author

saxenap commented Feb 18, 2015

. ./ini_helper "/home/ec2-user/helper_functions_for_bash" "/home/ec2-user/read_ini.sh"

readIniFile "/home/ec2-user/server_config"

parseVarsInSection "PhpPackagesToInstall"
_install "$(eachFrom INI__PhpPackagesToInstall[@])"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment