如何“x”匹配任何数组

时间:2018-02-15 23:40:22

标签: bash if-statement

我想知道如何使用select命令在bash中写入if语句,以便在给出非正确选项的情况下回显提示错误。

“$ strings变量是一列字符串。”

menu() {

strings=$(echo "$(awk -F'[][]' '{ print $(2) }' file | awk NF)")

    select string in $strings
        do
            if [[ "$string" MATCH ANY "${strings[@]}" ]]
                then
                    echo "you've selected $account"
                    break
                else
                    echo "Please, select one of the given options." && sleep 2
                    clear && menu
            fi
        done
}

我尝试过:

if [[ "$string" =~ "${strings[@]} ]]

但是不起作用。 有人能帮助我吗?

提前谢谢。

2 个答案:

答案 0 :(得分:0)

我找到了答案。这很简单,不想与其他任何东西进行比较,比如:

menu() {

    strings=$(echo "$(awk -F'[][]' '{ print $(2) }' file | awk NF)")

    select string in $strings
    do
        if [[ "$string" ]]
            then
                echo "you've selected $account"
                break
            else
                echo "Please, select one of the given options." && sleep 2
                clear && menu
        fi
    done
}

答案 1 :(得分:-1)

在您的代码中,您错过了if的结束引用。 它应该是:

if [[ "$string" =~ "${strings[@]}" ]]