56 lines
1.7 KiB
Bash
56 lines
1.7 KiB
Bash
|
#!/bin/bash
|
||
|
set -euo pipefail
|
||
|
|
||
|
|
||
|
## INIT ##
|
||
|
# get script base directory
|
||
|
SCRIPT_PATH=$(readlink -f "${BASH_SOURCE[0]}")
|
||
|
SCRIPT_DIR=$(dirname -- "$SCRIPT_PATH")
|
||
|
|
||
|
# load ini library
|
||
|
source "${SCRIPT_DIR}/../ini.sh"
|
||
|
|
||
|
## PARSE INI CONFIG ##
|
||
|
# `my_conf` is the prefix you can use to refer to this config's values
|
||
|
# defaults to `ini`
|
||
|
ini_parse "${SCRIPT_DIR}/config.ini" my_conf
|
||
|
|
||
|
# if you want to load another config file, you can just use a different name!
|
||
|
ini_parse "${SCRIPT_DIR}/other-config.ini" my_other_conf
|
||
|
|
||
|
|
||
|
|
||
|
## LIST ALL VALUES ##
|
||
|
# `my_conf` is an array of section names
|
||
|
for s in ${my_conf[*]}; do
|
||
|
echo "Section:: $s"
|
||
|
|
||
|
# `my_conf__section_1__key_list` is an array of keys under the section "section_1"
|
||
|
declare -n one_section_key_list="my_conf__${s}__key_list"
|
||
|
|
||
|
# `my_conf__section_1` is an associative array mapping keys to their values for the section "section_1"
|
||
|
declare -n one_section_map="my_conf__${s}"
|
||
|
|
||
|
for k in ${one_section_key_list[*]}; do
|
||
|
echo " Key:: $k"
|
||
|
echo " Value:: ${one_section_map[$k]}"
|
||
|
echo
|
||
|
done
|
||
|
echo
|
||
|
done
|
||
|
|
||
|
|
||
|
## READ CERTAIN VALUES ##
|
||
|
# get value of `first_value` under section `section_1`
|
||
|
echo "my_conf__section_1[first_value]:: ${my_conf__section_1[first_value]}"
|
||
|
|
||
|
# get value of `root_value` under root section (watch out for the triple underscore!)
|
||
|
echo "my_conf___root[root_value]:: ${my_conf___root[root_value]}"
|
||
|
|
||
|
# use bash parameter substitution for default values
|
||
|
echo "my_conf__section_1[third-value]:: ${my_conf__section_1[third-value]:-"<default value>"}"
|
||
|
echo "my_conf__section_1[fourth-value]:: ${my_conf__section_1[fourth-value]:-"<default value>"}"
|
||
|
|
||
|
# access values from other config file
|
||
|
echo "my_other_conf__sect[hihi]:: ${my_other_conf__sect[hihi]}"
|