Skip to content

Instantly share code, notes, and snippets.

@jceloria
Created June 23, 2014 17:58
Show Gist options
  • Save jceloria/91b9e2732d5d85d3a28c to your computer and use it in GitHub Desktop.
Save jceloria/91b9e2732d5d85d3a28c to your computer and use it in GitHub Desktop.
INI parser in pure bash4+
#!/bin/bash
CONFIG=$1
#### Assign associative arrays from ini config file: key[section]=value
while IFS='\n' read line; do
# Ignore comments and empty lines
[[ ${line} =~ (^[[:space:]]*$|^[\;x\#]) ]] && continue
# Are we a section heading?
if [[ ${line} == \[*] ]]; then
# add quotes witin the brackets
line=${line/#\[/\[\'} line=${line/%\]/\'\]}
section=${line}
# Or are we a key=value pair?
else
IFS='= ' read var val <<< ${line}
declare -A "${var}${section}=${val}"
fi
done < ${CONFIG}
PS3="Please make a selection: "
unset i; while [[ ! ${i} ]]; do
select i in "${!host[@]}" "New connection"; do
if [[ ${i} == "New connection" ]]; then
read -r -p 'Please enter domain\user@host: ' connection
RDP_DOMAIN=${1%%\\*}; RDP_HOST=${1##*@}
RDP_USER=${1##*\\};
[[ ${RDP_DOMAIN} == ${RDP_USER} ]] && unset RDP_DOMAIN
RDP_USER=${RDP_USER%@*}
else
SECTION=${i}
fi
break
done
[[ -z ${i} ]] && echo -e "\n* Invalid selection! *\n"
done
echo '${domain["${SECTION}"]}' = ${domain["${SECTION}"]}
echo '${host["${SECTION}"]}' = ${host["${SECTION}"]}
echo '${user["${SECTION}"]}' = ${user["${SECTION}"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment