I\'m new to bash scripting and have a question about using properties from a .properties file within a bash script.
I have seen a bash properties file that uses\'.\'
dot is not allowed to be variable name. so you cannot just simply source the property file.
What you can do is:
"parse" the file, not source it. E.g. with perl, awk or grep to get the value of interesting property name, and assign it to your shell var.
if you do want to set a var with dot in its name, you can use env 'a.b.c=xyz'
and get the a.b.c
from env
output.
No can do. The bash manual says this about variable names:
name
A word consisting solely of letters, numbers, and underscores, and beginning with a letter or underscore. Names are used as shell variable and function names. Also referred to as an identifier.
Dots not allowed.
Load them into an associative array. This will require your shell to be bash 4.x, not /bin/sh
(which, even when a symlink to bash, runs in POSIX compatibility mode).
declare -A props
while read -r; do
[[ $REPLY = *=* ]] || continue
props[${REPLY%%=*}]=${REPLY#*=}
done <input-file.properties
...after which you can access them like so:
echo "${props[this.prop.name]}"
If you want to recursively look up references, then it gets a bit more interesting.
getProp__property_re='[$][{]([[:alnum:].]+)[}]'
getProp() {
declare -A seen=( ) # to prevent endless recursion
declare propName=$1
declare value=${props[$propName]}
while [[ $value =~ $getProp__property_re ]]; do
nestedProp=${BASH_REMATCH[1]}
if [[ ${seen[$nestedProp]} ]]; then
echo "ERROR: Recursive definition encountered looking up $propName" >&2
return 1
fi
value=${value//${BASH_REMATCH[0]}/${props[$nestedProp]}}
done
printf '%s\n' "$value"
}
If we have props
defined as follows (which you could also get by running the loop at the top of this answer with an appropriate input-file.properties
):
declare -A props=(
[glassfish.home.dir]='${app.install.dir}/${glassfish.target}'
[app.install.dir]=/install
[glassfish.target]=target
)
...then behavior is as follows:
bash4-4.4$ getProp glassfish.home.dir
/install/target