日度归档:2018年5月27日

bash 查找数组中是否存在某个元素

realfilesarr=(123 456 789)

function contains() {
    local n=$#
    local value=${!n}
    for ((i=1;i < $#;i++)) {
        if [ "${!i}" == "${value}" ]; then
            echo "y"
            return 0
        fi
    }
    echo "n"
    return 1
}

if [ $(contains "${realfilesarr[@]}" "123") == "n" ]; then
    echo "不存在";
fi

if [ $contains "${realfilesarr[@]}" "123") == "y" ]; then
    echo "存在";
fi