判断某个值是否在数组里面

1、定义一个字典变量

declare -A dict
[hduser@yjt hive]$ dict['a']=1
[hduser@yjt hive]$ dict['b']=2
[hduser@yjt hive]$ dict['c']=3

2、判断某个值是否存在于字典的key集合里面
① 使用循环

[hduser@yjt hive]$ for i in ${!dict[*]};do if [ 'a' = $i ];then echo "yes" ;fi;done
yes
[hduser@yjt hive]$ for i in ${!dict[*]};do if [ 'd' = $i ];then echo "yes" ;fi;done
[hduser@yjt hive]$ 

② 使用shell 内置的方式

[hduser@yjt hive]$ if [[ ${!dict[*]/'a'/} != ${arr[*]} ]];then echo 'yes';fi
yes

借鉴:
https://www.cnblogs.com/thatsit/p/bash-shu-zupan-duan-mou-ge-yuan-su-shi-fou-zai-shu.html

原文地址:https://www.cnblogs.com/yjt1993/p/13206370.html