判斷某個值是否在數組里面


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