arrays - 如何检查数组的所有成员是否都等于 unix bash 中的某物

标签 arrays linux bash shell

有没有不用循环的方法来检查下面数组的所有成员是否都等于true

found1=(true true true true);

found2=(true false true true);

最佳答案

您可以使用 [[ ]] 运算符。这是一个基于函数的解决方案:

check_true() {
    [[ "${*}" =~ ^(true )*true$ ]]
    return
}

你可以这样使用它:

$ found1=(true true true true)
$ found2=(true false true true)
$ check_true ${found1[*]} && echo OK
OK
$ check_true ${found2[*]} && echo OK

OK will be displayed as a result if the condition satisfies

关于arrays - 如何检查数组的所有成员是否都等于 unix bash 中的某物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45991980/

相关文章:

java - 在Java中,如果每个线程写入单独的单元格空间,是否需要同步对数组的写入访问?

linux - shell脚本错误: "head: invalid trailing option -- 1"

xml - 如何使用 Bash 解析 XML 文件中的某些标记并将它们存储在数组中?

bash - 进行字符串插值时如何转义右括号

arrays - Bash 数组名称选择

arrays - 使用曼伯迈尔斯算法的后缀数组

Python 数组求和与 MATLAB

linux - 使用 sed 将大写字母替换为小写字母,将空格替换为下划线,然后创建一个带有结果的 xml 文件

linux - 为什么 Doxygen 会创建这么多空文件夹?

bash - 如果未设置变量,则在 Bash 中为变量分配默认值