检查Bash中的列表中是否存在变量

您所在的位置:网站首页 脚本判断变量name是否存在 检查Bash中的列表中是否存在变量

检查Bash中的列表中是否存在变量

2024-06-15 12:57| 来源: 网络整理| 查看: 265

1. 概述

当我们在变量中有一个元素列表时,有时我们想检查该列表中是否存在特定元素。不幸的是,Bash 没有提供内置函数来做到这一点。

在本教程中,我们将了解如何检查变量是否存在于列表中。为此,我们将学习使用for 遍历列表,使用grep 以及使用带有Bash的*[[]]* 的正则表达式。

2. 问题示例

让我们从一个名为list_of_fruits的水果列表开始:

$ list_of_fruits="banana pear apple strawberry lime"

在这种情况下,我们使用空格作为项目分隔符。但是,我们可以选择任何字符作为分隔符。现在,我们要检查列表中是否存在变量。

**假设有一个名为exists_in_list的函数,它检查一个元素是否存在于列表中。**该函数接收列表、项目分隔符和元素作为参数。此外,如果元素存在于列表中,此函数应返回 0,否则返回 1。

在 Bash 中,我们在执行成功时返回 0,在这种情况下,当我们找到该项目时。否则,当执行不成功时,我们返回任何其他非 0 值,在这种情况下,当我们没有找到该项目时。这些返回值遵循标准 Bash 退出代码,使得在if条件中调用函数exists_in_list变得容易。

我们将使用exists_in_list函数以这种方式调用它:

$ exists_in_list "$LIST" "$DELIMINTER" "$ELEMENT"

然后,让我们看看如何检查列表list_of_fruits中水果“pear”和“raspberry”的存在,并使用空格分隔项目:

$ if exists_in_list "$list_of_fruits" " " pear; then echo "pear is in the list" else echo "pear is not in the list" fi $ if exists_in_list "$list_of_fruits" " " raspberry; then echo "raspberry is in the list" else echo "raspberry is not in the list" fi

在以下部分中,我们将使用不同的方法定义函数exists_in_list 。

3. 遍历列表

检查元素是否存在于列表中的一种方法是遍历列表。我们可以使用for循环来迭代列表。

for循环以 for VARIABLE in LIST 的形式接收一个变量和一个list。当我们这样做时,for将遍历LIST并将每个元素分配给VARIABLE。让我们看看它是如何工作的:

$ for x in element1 element2 element3; do echo $x done element1 element2 element3

我们可以看到,for循环遍历项目,将每个项目分配给变量x。列表项必须用空格分隔,以便for可以分隔它们。

现在,我们可以检查一个元素是否存在于for循环中,将每个元素与所需的值进行比较。如果我们找到项目“ element2 ”,让我们迭代列表并打印一条消息:

$ { for x in element1 element2 element3; do if [ $x = "element2" ]; then echo "The element was found" fi done } The element was found

我们的输入列表可以使用任何字符串作为分隔符。因此,我们必须用空格替换项目分隔符。为此,我们可以使用tr 命令。我们将echo该列表并通过*tr DELIMITER ““将其传递给管道 ,并将结果分配给一个新变量。

**最后,我们可以定义函数exists_in_list。**让我们接收元素列表作为第一个参数,分隔符作为第二个参数,以及要搜索的值作为第三个参数:

$ function exists_in_list() { LIST=$1 DELIMITER=$2 VALUE=$3 LIST_WHITESPACES=`echo $LIST | tr "$DELIMITER" " "` for x in $LIST_WHITESPACES; do if [ "$x" = "$VALUE" ]; then return 0 fi done return 1 }

我们可以看到,我们首先转换输入列表,以便我们可以  在for循环中使用变量LIST_WHITESPACES。然后,如果找到该项目,该函数将返回值 0。否则,函数返回值 1。

让我们运行上一节中的示例:

$ if exists_in_list "$list_of_fruits" " " pear; then echo "pear is in the list" else echo "pear is not in the list" fi pear is in the list $ if exists_in_list "$list_of_fruits" " " raspberry; then echo "raspberry is in the list" else echo "raspberry is not in the list" fi raspberry is not in the list 4. 使用grep

我们还可以利用grep的功能在单词列表中查找单词。

首先,我们将用换行符替换项目分隔符。然后,我们可以将用换行符分隔的列表通过管道传递给grep -F -q -x “$VALUE”。这样,我们检查VALUE是否在列表中。

让我们回顾一下我们正在使用的grep参数:

-F:这会禁用任何正则表达式功能,因此即使有 * 或 + 之类的字符, grep也会搜索文字字符串 -q:这会启用安静模式,因此grep不会打印任何消息 -x:这会将 grep配置为仅查找整行匹配的匹配项

要用新行替换分隔符,我们可以再次使用tr。为此,我们可以echo列表并将其通过*tr DELIMITER ‘\n’*进行管道传输。

现在,让我们使用grep方法定义函数exists_in_list:

$ function exists_in_list() { LIST=$1 DELIMITER=$2 VALUE=$3 echo $LIST | tr "$DELIMITER" '\n' | grep -F -q -x "$VALUE" }

在这种情况下,我们不需要使用return命令。exists_in_list函数将返回与最后执行的return 相同的值,在本例中为grep。

让我们看看它是如何工作的:

$ if exists_in_list "$list_of_fruits" " " pear; then echo "pear is in the list" else echo "pear is not in the list" fi pear is in the list $ if exists_in_list "$list_of_fruits" " " raspberry; then echo "raspberry is in the list" else echo "raspberry is not in the list" fi raspberry is not in the list

默认情况下,grep匹配任何出现,无论是整个单词还是其中的一部分。但是,我们使用*-x*参数仅匹配整行。

让我们测试搜索“berry”的函数,它不在我们的列表中,但在“strawberry”项中:

$ if exists_in_list "$list_of_fruits" " " berry; then echo "berry is in the list"; else echo "berry is not in the list"; fi berry is not in the list

正如我们所料,列表中没有找到“浆果”。

5. 在 Bash 的*[[ ]]*中使用正则表达式

我们还有另一种方法来检查列表中是否存在变量。*我们可以在 Bash 的[[ ]]*中使用正则表达式来确定列表是否包含项目。

首先,让我们回顾一下我们需要哪些条件来检查列表是否包含该项目:

如果项目在列表中间,则项目前后有分隔符 当列表仅包含项目时,列表以项目开始和结束 如果该项是列表中的第一项,则列表以该项开始,项后有分隔符 最后,如果该项是列表中的最后一项,则列表以该项结束,项前有分隔符

现在,我们可以编写一个正则表达式来涵盖所有这些情况:

($DELIMITER|^)$VALUE($DELIMITER|$)

我们可以使用*[[ STRING =~ REGEX ]]在 Bash 中使用正则表达式。如果正则表达式匹配,此命令返回值 0。否则,它返回值 1。由于我们需要在exists_in_list函数中具有这种行为,我们可以运行[[ ]]命令而无需任何显式return*命令。

让我们用这个方法重写exists_in_list函数:

$ function exists_in_list() { LIST=$1 DELIMITER=$2 VALUE=$3 [[ "$LIST" =~ ($DELIMITER|^)$VALUE($DELIMITER|$) ]] }

让我们检查list_of_fruits列表中是否存在“pear”和“raspberry” :

$ if exists_in_list "$list_of_fruits" " " pear; then echo "pear is in the list" else echo "pear is not in the list" fi pear is in the list $ if exists_in_list "$list_of_fruits" " " raspberry; then echo "raspberry is in the list" else echo "raspberry is not in the list" fi raspberry is not in the list


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3