mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-06-08 16:52:31 +02:00
docs: update
This commit is contained in:
parent
e84582ca6b
commit
396d540dd0
14
README.md
14
README.md
|
@ -334,6 +334,8 @@ Hello, World
|
||||||
|
|
||||||
## Parsing a `key=val` file.
|
## Parsing a `key=val` file.
|
||||||
|
|
||||||
|
This could be used to parse a simple `key=value` configuration file.
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
# Setting 'IFS' tells 'read' where to split the string.
|
# Setting 'IFS' tells 'read' where to split the string.
|
||||||
while IFS='=' read -r key val; do
|
while IFS='=' read -r key val; do
|
||||||
|
@ -344,6 +346,18 @@ while IFS='=' read -r key val; do
|
||||||
# '$key' stores the key.
|
# '$key' stores the key.
|
||||||
# '$val' stores the value.
|
# '$val' stores the value.
|
||||||
printf '%s: %s\n' "$key" "$val"
|
printf '%s: %s\n' "$key" "$val"
|
||||||
|
|
||||||
|
# Alternatively replacing 'printf' with the following
|
||||||
|
# populates variables called '$key' with the value of '$val'.
|
||||||
|
#
|
||||||
|
# NOTE: I would extend this with a check to ensure 'key' is
|
||||||
|
# a valid variable name.
|
||||||
|
# export "$key=$val"
|
||||||
|
#
|
||||||
|
# Example with error handling:
|
||||||
|
#
|
||||||
|
# export "$key=$val" 2>/dev/null ||
|
||||||
|
# printf 'warning %s is not a valid variable name\n' "$key"
|
||||||
done < "file"
|
done < "file"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue