如何检查 Bash 脚本中是否存在关联数组元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30749421/
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 13:09:42  来源:igfitidea点击:

How can I check if an associative array element exists in my Bash script?

bash

提问by sensorario

I have an array of animals:

我有一系列动物:

declare -A animals=()
animals+=([horse])

and I want to check if an animal exists or not:

我想检查动物是否存在:

if [ -z "$animals[horse]"]; then
    echo "horse exists";
fi

but this does not work.

但这不起作用。

回答by chepner

In bash4.3, the -voperator can be applied to arrays.

bash4.3 中,-v运算符可以应用于数组。

declare -A animals
animals[horse]=neigh
# Fish are silent
animals[fish]=
[[ -v animals[horse] ]] && echo "horse exists"
[[ -v animals[fish] ]] && echo "fish exists"
[[ -v animals[unicorn] ]] && echo "unicorn does not exist" 

In prior versions, you would need to be more careful distinguishing between the key not existing and the key referring to any empty string.

在以前的版本中,您需要更加小心地区分不存在的键和引用任何空字符串的键。

animal_exists () {
    # If the given key maps to a non-empty string (-n), the
    # key obviously exists. Otherwise, we need to check if
    # the special expansion produces an empty string or an
    # arbitrary non-empty string.
    [[ -n ${animals[]} || -z ${animals[]-foo} ]]
}

animal_exists horse && echo "horse exists"
animal_exists fish && echo "fish exists"
animal_exists unicorn || echo "unicorn does not exist"

回答by Dmitri Chubarov

There are several typos in your script

你的脚本中有几个错别字

When I run it as it is, I get the following error messages from BASH:

当我按原样运行它时,我从 BASH 收到以下错误消息:

1. animals: [horse]: must use subscript when assigning associative array
2. [: missing `]'

The first one says that if you want to use horseas an index to an associative array, you have to assign a value to it. An empty value (null) is ok.

第一个说如果你想horse用作关联数组的索引,你必须给它赋值。空值(null)是可以的。

-animals+=([horse])
+animals+=([horse]=)

The second message says that you need to separate the value you want to test and the bracket, as square bracket is considered a part of the value if not separated by spaces

第二条消息说您需要将要测试的值和括号分开,因为如果不以空格分隔,方括号将被视为值的一部分

-if [ -z "$animals[horse]"]; then
+if [ -z "$animals[horse]" ]; then

Finally, an element in an associative array exists when there is a value assigned to it (even if this value is null). As the question of testing if an array value is set has already been answered on this site, we can borrow the solution

最后,关联数组中的元素在有赋值时就存在(即使该值为空)。由于测试数组值是否设置的问题已经在本站回答,我们可以借用解决方案。

-if [ -z "$animals[horse]"]; then
+if [ -n "${animals[horse]+1}" ]; then


For your convinience here is the complete script:

为了您的方便,这里是完整的脚本:

declare -A animals=()
animals+=([horse]=)

if [ -n "${animals[horse] + 1}" ]; then
    echo "horse exists";
fi

回答by anubhava

In BASH you can do:

在 BASH 中,您可以执行以下操作:

declare -A animals=()
animals+=([horse]=)

[[ "${animals[horse]+foobar}" ]] && echo "horse exists"

"${animals[horse]+foobar}"returns foobarif horseis a valid index in array otherwise it returns nothing.

"${animals[horse]+foobar}"返回foobar如果horse是在阵列中的有效指标,否则将没有返回值。