有没有不用循环的方法来检查下面数组的所有成员是否都等于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/