使用“.”的 Bash 脚本属性文件 在变量名中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21591596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:28:47  来源:igfitidea点击:

Bash Script Properties File Using '.' in Variable Name

linuxbashscriptingproperties-file

提问by Matt

I'm new to bash scripting and have a question about using properties from a .properties file within a bash script.

我是 bash 脚本的新手,有一个关于在 bash 脚本中使用 .properties 文件中的属性的问题。

I have seen a bash properties file that uses'.' between variable names, for example:

我见过一个使用 '.' 的 bash 属性文件。变量名之间,例如:

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}

I would really like to be able to use '.' in the variable names, and, if at all possible, not have to include . test.propertiesin the script.

我真的很想能够使用 '.' 在变量名称中,并且,如果可能的话,不必包含. 脚本中的test.properties

Is this possible?

这可能吗?

UPDATE:

更新:

Thanks for your answers! Well, then this is strange. I'm working with a bash script that looks like this (a service for glassfish):

感谢您的回答!那么,这很奇怪。我正在使用一个看起来像这样的 bash 脚本(一种用于 glassfish 的服务):

#!/bin/bash

start() {
        sudo ${glassfish.home.dir}/bin/asadmin start-domain domain1
}

...

...and there are property files like this (build.properties):

...还有这样的属性文件(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.

那么,一定有办法做到这一点吗?如果它们是在属性文件中声明的,它们是否可能不被定义为“变量”?再次感谢。

回答by Charles Duffy

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).

将它们加载到关联数组中。这将要求您的 shell 是 bash 4.x,而不是/bin/sh(即使是 bash 的符号链接,它也以 POSIX 兼容模式运行)。

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=
  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 propsdefined as follows (which you could also get by running the loop at the top of this answer with an appropriate input-file.properties):

如果我们props定义如下(您也可以通过使用适当的 运行此答案顶部的循环来获得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

回答by glenn Hymanman

No can do. The bash manual says this about variable names:

没办法。bash 手册对变量名是这样说的:

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.

不允许点。

回答by Kent

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.

“解析”文件,而不是源文件。例如,使用 perl、awk 或 grep 来获取感兴趣的属性名称的值,并将其分配给您的 shell 变量。

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.cfrom envoutput.

如果您确实想设置名称中带有点的 var,则可以使用env 'a.b.c=xyz'并获取a.b.cfromenv输出。