bash 如何删除bash数组中的确切元素?

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

How to delete an exact element in a bash array?

arraysbashunset

提问by mateor

I am trying to remove just the first appearanceof any one keyword from a bash array.

我试图从 bash 数组中删除任何一个关键字的第一次出现

ARRAY=(foo bar and any number of keywords)
keywords=(red, rednet, rd3.0)

I remove the keyword like this: ARRAY=( ${ARRAY[@]/"$keyword"/} )then if "red' is the first found keyword, it will strip 'red' from both keywords and return "foo bar net" instead of "foo bar rednet".

我像这样删除关键字:ARRAY=( ${ARRAY[@]/"$keyword"/} )然后如果“red”是第一个找到的关键字,它将从两个关键字中删除“ red”并返回“foo bar net”而不是“foo bar rednet”。

Edit: Here is example, hopefully this makes it clearer.

编辑:这是示例,希望这能让它更清楚。

for keyword in ${ARRAY[@]}; do
      if [ "$keyword" = "red" ] || [ "$keyword" = "rd3.0" ] || [ "$keyword" = "rednet" ]; then
           # HERE IS TROUBLE
           ARRAY=( ${ARRAY[@]/"$keyword"/} )
           echo "ARRAY is now ${ARRAY[@]}"
           break
      fi
 done

Which if the ARRAY=(red rednet rd3.0)returns net rd3.0instead of rednet rd3.0

如果ARRAY=(red rednet rd3.0)返回net rd3.0而不是rednet rd3.0

If I use unset,: unset ${ARRAY["$keyword"]}bash complains if the rd3.0 is in the array: :syntax error: invalid arithmetic operator (error token is ".0")What is the safe way to unset or remove just an exact match from an array?

如果我使用 unset,unset ${ARRAY["$keyword"]}bash 会抱怨 rd3.0 是否在数组中::syntax error: invalid arithmetic operator (error token is ".0")从数组中取消设置或删除完全匹配的安全方法是什么?

回答by higuaro

Use the unsetcommand with the array value at index, something like this:

使用unset数组值为 at的命令index,如下所示:

#!/usr/bin/env bash
ARRAY=(foo bar any red alpha number of keywords rd3.0 and)
keywords=(red, rednet, rd3.0)

index=0
for keyword in ${ARRAY[@]}; do
      if [ "$keyword" = "red" ] || [ "$keyword" = "rd3.0" ] || [ "$keyword" = "rednet" ]; then
           # HERE IS TROUBLE
           # ARRAY=( ${ARRAY[@]/"$p"/} )
           unset ARRAY[$index]
           echo "ARRAY is now: ${ARRAY[@]}"
           break
      fi
      let index++
 done

回答by erik

First: You should use quotation marks around your keys in the arrays. This avoids problems with for example rd3.0.

第一:您应该在数组中的键周围使用引号。这避免了例如 rd3.0 的问题。

Like that:

像那样:

ARRAY=("foo" "bar" "and" "any" "number" "of" "keywords")
keywords=("red", "rednet", "rd3.0")

In my opinion you need to copy the array and then use a for loop to filter the keywords. Exit the for loop after the first successful filtering. After that copy it back without the empty array elements. See thisshort examples (paragraph 10).

在我看来,您需要复制数组,然后使用 for 循环来过滤关键字。第一次过滤成功后退出 for 循环。之后将它复制回没有空的数组元素。请参阅简短示例(第 10 段)。

More on arrays: http://tldp.org/LDP/abs/html/arrays.html(everything you will ever need)

有关阵列的更多信息:http: //tldp.org/LDP/abs/html/arrays.html(您将需要的一切)