SHTK_CONFIG(3)
SHTK_CONFIG(3) Library Functions Manual SHTK_CONFIG(3)

configConfiguration file processing and queries

shtk_import config

The config module provides a mechanism to load configuration files and to handle any settings defined in them.

Configuration files are simply shell scripts that can define a set of predefined variables.

Consider the following configuration file:

MYVAR1="this is the first setting"
MYVAR2="this is the second setting"

post_load_hook() {
    echo "Custom hook! The file has been loaded!"
}

The following sample code depicts a main method to load the file above, including support for specifying the path to the configuration file and support to accept command-line overrides to the configuration variables:

local config_file="/etc/foo.conf"

shtk_config_init MYVAR1 MYVAR2

local OPTIND
while getopts ':c:o:' arg "${@}"; do
    case "${arg}" in
        c)  # Path to the configuration to load.
            config_file="${OPTARG}"
            ;;

        o)  # Override for a particular configuration variable.
            shtk_config_override "${OPTARG}"
            ;;

        \?)
            shtk_cli_usage_error "Unknown option -${OPTARG}"
            ;;
    esac
done
shift $((${OPTIND} - 1))

shtk_config_set MYVAR1 "default value for first variable"
# No default value for MYVAR2
post_load_hook() { true; }

shtk_config_load "${config_file}"

echo "MYVAR1 is $(shtk_config_get MYVAR1)"
echo "MYVAR2 is $(shtk_config_get_default MYVAR2 "not yet set")"
shtk_config_run_hook post_load_hook

shtk(3), shtk_config_get(3), shtk_config_get_bool(3), shtk_config_get_default(3), shtk_config_has(3), shtk_config_include(3), shtk_config_init(3), shtk_config_load(3), shtk_config_override(3), shtk_config_run_hook(3), shtk_config_set(3), shtk_config_unset(3)

config first appeared in shtk 1.0.

November 6, 2014 Debian