linux - Bash Script Properties File Using '.' in Variable Name -
i'm new bash scripting , have question using properties .properties file within bash script.
i have seen bash properties file uses'.' between variable names, example:
this.prop.one=someproperty
and i've seen them called within script like:
echo ${this.prop.one}
but when try set property error:
./test.sh: line 5: ${this.prop.one}: bad substitution
i can use properties if without '.' in variable names, , include props file:
#!/bin/bash . test.properties echo ${this_prop_one}
i able use '.' in variable names, and, if @ possible, not have include . test.properties in script.
is possible?
update:
thanks answers! well, strange. i'm working bash script looks (a service glassfish):
#!/bin/bash start() { sudo ${glassfish.home.dir}/bin/asadmin start-domain domain1 } ...
...and there property files (build.properties):
# glassfish glassfish.version=2.1 glassfish.home.dir=${app.install.dir}/${glassfish.target} ...
so, there must way of doing right? these maybe not considered 'variables' definition if they're declared in properties file? again.
load them associative array. require shell bash 4.x, not /bin/sh
(which, when symlink bash, runs in posix compatibility mode).
declare -a props while read -r; [[ $reply = *=* ]] || continue props[${reply%%=*}]=${reply#*=} done <input-file.properties
...after can access them so:
echo "${props[this.prop.name]}"
if want recursively references, gets bit more interesting.
getprop__property_re='[$][{]([[:alnum:].]+)[}]' getprop() { declare -a seen=( ) # prevent endless recursion declare propname=$1 declare value=${props[$propname]} while [[ $value =~ $getprop__property_re ]]; nestedprop=${bash_rematch[1]} if [[ ${seen[$nestedprop]} ]]; echo "error: recursive definition encountered looking $propname" >&2 return 1 fi value=${value//${bash_rematch[0]}/${props[$nestedprop]}} done printf '%s\n' "$value" }
if have props
defined follows (which running loop @ top of answer appropriate input-file.properties
):
declare -a props=( [glassfish.home.dir]='${app.install.dir}/${glassfish.target}' [app.install.dir]=/install [glassfish.target]=target )
...then behavior follows:
bash4-4.4$ getprop glassfish.home.dir /install/target
Comments
Post a Comment