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