Skip to content

Instantly share code, notes, and snippets.

@ExploitSage
Created February 5, 2020 15:31
Show Gist options
  • Save ExploitSage/7c92a791cf9d583254754ec6ac6786ed to your computer and use it in GitHub Desktop.
Save ExploitSage/7c92a791cf9d583254754ec6ac6786ed to your computer and use it in GitHub Desktop.
function parse_ini() {
local varname="${1}"
local ini_source=$(cat -)
declare -gA "${varname}"
local IFS_BAK="${IFS}";
IFS=$'\n';
for section in $(echo "${ini_source}" | grep -Po '(?<=\[).*(?=\])'); do
for option_value in $(echo "${ini_source}" | grep -Pazo "(?s)(?<=\[${section}\])(.*?)(?=\[|$)" | tr -d '\000' | grep "="); do
option="$(echo "${option_value}" | cut -d "=" -f 1)";
value="$(echo "${option_value}" | cut -d "=" -f 2-)";
eval "${varname}[\"${section},${option}\"]=\"${value}\"";
done;
done;
IFS="${IFS_BAK}";
}
# TODO Need easy way to take output into array without sections with "spaces" being split-up
function get_sections() {
IFS_BAK="${IFS}";
IFS=$'\n';
for key in "${@}"; do
section_key="$(echo "${key}" | cut -d ',' -f 1)"
echo "${section_key}"
done | sort | uniq;
IFS="${IFS_BAK}";
}
function get_options() {
section="${1}";
shift 1;
IFS_BAK="${IFS}";
IFS=$'\n';
for key in "${@}"; do
echo "${key}" | grep "${section}";
done | sort;
IFS="${IFS_BAK}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment