Configuration Files
Key-Value Config
Reading a Setting
Many simple config files use name=value lines. A script can read each line and keep the value for the key it needs.
Program
Play the script to scan fixed config text for a selected key.
parse_key_values.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
config=$'host=local\nport=8080\ndebug=false'
selected_key="port"
selected_value=""
while IFS="=" read -r key value; do
if [[ "$key" == "$selected_key" ]]; then
selected_value=$value
fi
done <<< "$config"
echo "$selected_key=$selected_value"
#!/usr/bin/env bash
config=$'host=local\nport=8080\ndebug=false'
selected_key="host"
selected_value=""
while IFS="=" read -r key value; do
if [[ "$key" == "$selected_key" ]]; then
selected_value=$value
fi
done <<< "$config"
echo "$selected_key=$selected_value"
#!/usr/bin/env bash
config=$'host=local\nport=8080\ndebug=false'
selected_key="debug"
selected_value=""
while IFS="=" read -r key value; do
if [[ "$key" == "$selected_key" ]]; then
selected_value=$value
fi
done <<< "$config"
echo "$selected_key=$selected_value"
config ← host, port, debug
3config=$'host=local\nport=8080\ndebug=false'4selected_key="port"values this stephost, port, debugconfigselected_key ← port
3config=$'host=local\nport=8080\ndebug=false'4selected_key="port"5selected_value=""values this stepportselected_keyselected_value ← (empty)
4selected_key="port"5selected_value=""6while IFS="=" read -r key value; dovalues this step(empty)selected_valuekey ← host, value ← local
5selected_value=""6while IFS="=" read -r key value; do7 if [[ "$key" == "$selected_key" ]]; thenvalues this stephostkeylocalvaluekey ← port, value ← 8080
5selected_value=""6while IFS="=" read -r key value; do7 if [[ "$key" == "$selected_key" ]]; thenvalues this stepportkey8080valueselected_value ← 8080
7if [[ "$key" == "$selected_key" ]]; then8 selected_value=$value9fivalues this step8080selected_valueportkey8080valuekey ← debug, value ← false
5selected_value=""6while IFS="=" read -r key value; do7 if [[ "$key" == "$selected_key" ]]; thenvalues this stepdebugkeyfalsevalueecho "$selected_key=$selected_value"
10done <<< "$config"11echo "$selected_key=$selected_value"outputport=8080values this stepportselected_key8080selected_value
config ← host, port, debug
3config=$'host=local\nport=8080\ndebug=false'4selected_key="host"values this stephost, port, debugconfigselected_key ← host
3config=$'host=local\nport=8080\ndebug=false'4selected_key="host"5selected_value=""values this stephostselected_keyselected_value ← (empty)
4selected_key="host"5selected_value=""6while IFS="=" read -r key value; dovalues this step(empty)selected_valuekey ← host, value ← local
5selected_value=""6while IFS="=" read -r key value; do7 if [[ "$key" == "$selected_key" ]]; thenvalues this stephostkeylocalvalueselected_value ← local
7if [[ "$key" == "$selected_key" ]]; then8 selected_value=$value9fivalues this steplocalselected_valuehostkeylocalvaluekey ← port, value ← 8080
5selected_value=""6while IFS="=" read -r key value; do7 if [[ "$key" == "$selected_key" ]]; thenvalues this stepportkey8080valuekey ← debug, value ← false
5selected_value=""6while IFS="=" read -r key value; do7 if [[ "$key" == "$selected_key" ]]; thenvalues this stepdebugkeyfalsevalueecho "$selected_key=$selected_value"
10done <<< "$config"11echo "$selected_key=$selected_value"outputhost=localvalues this stephostselected_keylocalselected_value
config ← host, port, debug
3config=$'host=local\nport=8080\ndebug=false'4selected_key="debug"values this stephost, port, debugconfigselected_key ← debug
3config=$'host=local\nport=8080\ndebug=false'4selected_key="debug"5selected_value=""values this stepdebugselected_keyselected_value ← (empty)
4selected_key="debug"5selected_value=""6while IFS="=" read -r key value; dovalues this step(empty)selected_valuekey ← host, value ← local
5selected_value=""6while IFS="=" read -r key value; do7 if [[ "$key" == "$selected_key" ]]; thenvalues this stephostkeylocalvaluekey ← port, value ← 8080
5selected_value=""6while IFS="=" read -r key value; do7 if [[ "$key" == "$selected_key" ]]; thenvalues this stepportkey8080valuekey ← debug, value ← false
5selected_value=""6while IFS="=" read -r key value; do7 if [[ "$key" == "$selected_key" ]]; thenvalues this stepdebugkeyfalsevalueselected_value ← false
7if [[ "$key" == "$selected_key" ]]; then8 selected_value=$value9fivalues this stepfalseselected_valuedebugkeyfalsevalueecho "$selected_key=$selected_value"
10done <<< "$config"11echo "$selected_key=$selected_value"outputdebug=falsevalues this stepdebugselected_keyfalseselected_value
Follow the Key
- The config text has
host=local,port=8080, anddebug=false. selected_keystarts asport.- The loop reads one
key=valueline at a time. - Only the
portline matches, soselected_valuebecomes8080. - The script prints
port=8080. | selected_key | matching line | selected_value | output | | --- | --- | --- | --- | | host | host=local | local | host=local | | port | port=8080 | 8080 | port=8080 | | debug | debug=false | false | debug=false |
IFS
`IFS="="` makes `read` split each line around the equals sign.
read loop
`while read ...; do` processes one config line per iteration.
selected key
The `if` keeps only the value whose key matches the selected setting.
Exercise: parse_key_values.sh
Reproduce port=8080, then use selected_key host and debug to predict host=local and debug=false.