shell查找数组是否有特定的值

arr_host=("abc" "123" "tom")
find_value="abc"

方法1

res=$(echo "${arr_host[@]}" | grep -wq "${find_value}" &&  echo "yes" || echo "no")

方法2

function array_have_value() {
    for i in "$2"
    do
        [ "$1" == "$i" ] && echo "yes" || echo "no"
    done
}

res=$(array_have_value "${arr_host[@]}" "${find_value}")
if [ "${res}" == "yes" ];then
    echo "have"
else
    echo "no"
fi
原文地址:https://www.cnblogs.com/smallredness/p/13502872.html