Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何检查数组中是否包含除bash中特定的两个字符值以外的任何内容?_Arrays_Bash - Fatal编程技术网

Arrays 如何检查数组中是否包含除bash中特定的两个字符值以外的任何内容?

Arrays 如何检查数组中是否包含除bash中特定的两个字符值以外的任何内容?,arrays,bash,Arrays,Bash,守则: [[ " ${arr[*]} " == *" "[^a]" "* ]] && echo "array has non-a element" || echo "All a elements" 如果我试图查看是否存在不包含“a”的数组元素,那么这个方法非常有效 例如: arr=(a c) 返回: 数组具有非a元素 我想修改上面的代码,以查看数组中是否存在不包含“CA”的元素,该数组如下所示: arr=(CA-AC) 或 arr=(CA BC AB) 或 (KL CA)只需使

守则:

[[ " ${arr[*]} " == *" "[^a]" "* ]] && echo "array has non-a element" || echo "All a elements"
如果我试图查看是否存在不包含“a”的数组元素,那么这个方法非常有效

例如: arr=(a c)

返回: 数组具有非a元素

我想修改上面的代码,以查看数组中是否存在不包含“CA”的元素,该数组如下所示:

arr=(CA-AC)

arr=(CA BC AB)


(KL CA)

只需使用
=

[[ "${arr[*]}" != *CA* ]]

您可以将
printf
grep-zEqv
(gnu grep)一起使用:

$ arr=(CA CA CA BC AB)
$ printf "%s\0" "${arr[@]}" | grep -zEvq "^CA$" &&
     echo "array has non-CA element" || echo "All CA elements"
array has non-CA element

$ arr=( CA CA CA $'CA\nCA' )
$ printf "%s\0" "${arr[@]}" | grep -zEvq "^CA$" &&
     echo "array has non-CA element" || echo "All CA elements"
array has non-CA element

$ arr=(CA CA CA)
$ printf "%s\0" "${arr[@]}" | grep -zEvq "^CA$" &&
     echo "array has non-CA element" || echo "All CA elements"
All CA elements