我如何检查数组是否包含bash中特定值以外的其他内容? [英] How do I check if an array contains anything else than a specific value in bash?

查看:39
本文介绍了我如何检查数组是否包含bash中特定值以外的其他内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

arr=( d d d a)

下面的代码应该检查它是否包含 d .有没有类似的方法来检查它是否包含除 d 之外的其他内容?

The code below is supposed to check if it contains d. Is there a similar way to check if it contains anything else than d?

if [[ " ${arr[*]} " == *" d "* ]]; then                                         
 echo "arr contains d"  
fi

推荐答案

只要您不在Array元素中使用空格,就可以使用:

As long as you're not using space in Array elements you can use:

[[ " ${arr[*]} " == *" "[^d]" "* ]] && echo "array has non-d element" || echo "no"

测试:

arr=(d d d a)
[[ " ${arr[*]} " == *" "[^d]" "* ]] && echo "array has non-d element" || echo "no"
array has non-d element

arr=(d d d)
[[ " ${arr[*]} " == *" "[^d]" "* ]] && echo "array has non-d element" || echo "no"
no

这篇关于我如何检查数组是否包含bash中特定值以外的其他内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆