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


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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM