shell 中判断值是否已经在数组中

1. echo "${array[@]}" | grep -wq "$var" &&echo "Yes" || echo "No"
2. for i in ${array[@]}
do
[ "$i" == "$var" ] && echo "yes"
done
3.

  1. test_array ()
  2. {
  3. y_n=$1

  4. arr=$2

  5. echo $(eval echo \${$arr[@]}) |\
  6. sed 's/ /\n/g' |\
  7. while read line
  8. do

  9. if [ "$y_n" == "$line" ]
  10. then
  11. return 1
  12. break
  13. else
  14. continue
  15. fi
  16. ((i++))
  17. done
  18. }

  19. test_array $var array


    推荐阅读