wor*_*din 4 linux arrays bash shell
有没有没有循环的方法来检查以下数组的所有成员是否等于true?
found1=(true true true true);
found2=(true false true true);
Run Code Online (Sandbox Code Playgroud)
您可以使用[[ ]]运算符。这是一个基于函数的解决方案:
check_true() {
[[ "${*}" =~ ^(true )*true$ ]]
return
}
Run Code Online (Sandbox Code Playgroud)
您可以这样使用它:
$ found1=(true true true true)
$ found2=(true false true true)
$ check_true ${found1[*]} && echo OK
OK
$ check_true ${found2[*]} && echo OK
Run Code Online (Sandbox Code Playgroud)
满足条件则显示OK