bash脚本属性文件中使用“。”在变量名 [英] Bash Script Properties File Using '.' in Variable Name
问题描述
我是新来的bash脚本和有一个关于使用从性能的.properties问题bash脚本内提交。
I'm new to bash scripting and have a question about using properties from a .properties file within a bash script.
我已经看到了使用一个bash属性文件'。变量名之间,例如:
I have seen a bash properties file that uses'.' between variable names, for example:
this.prop.one=someProperty
和我见过他们就像一个脚本中调用:
and I've seen them called from within a script like:
echo ${this.prop.one}
但是,当我尝试设置这个属性我得到一个错误:
But when I try to set this property I get an error:
./test.sh: line 5: ${this.prop.one}: bad substitution
如果我这样做,我没有可以使用的属性'。在变量名称,包括道具文件:
I can use properties if I do it without '.' in the variable names, and include the props file:
#!/bin/bash
. test.properties
echo ${this_prop_one}
我真的希望能够使用'。在变量名,并且,如果可能的话,不一定要包括的。 test.properties脚本的
这可能吗?
更新:
谢谢您的回答!好吧,那么这很奇怪。
Thanks for your answers! Well, then this is strange. I'm working with a bash script that looks like this (a service for glassfish):
#!/bin/bash
start() {
sudo ${glassfish.home.dir}/bin/asadmin start-domain domain1
}
...
...而且有属性文件这样的(build.properties):
...and there are property files like this (build.properties):
# glassfish
glassfish.version=2.1
glassfish.home.dir=${app.install.dir}/${glassfish.target}
...
因此,必须有这样做的权利的某种方式?这些也许不是视为变量的定义,如果他们在属性文件中在声明?再次感谢。
So, there must be some way of doing this right? Are these maybe not considered 'variables' by definition if they're declared in a properties file? Thanks again.
推荐答案
加载它们变成一个关联数组。这需要你的shell是bash的4.x的,而不是 / bin / sh的
(其中,甚至当一个符号链接来砸在POSIX兼容模式运行)。
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]}"
这篇关于bash脚本属性文件中使用“。”在变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!