如果输入选项不正确,如何使 bash 中的菜单出现错误

如果输入选项不正确,如何使 bash 中的菜单出现错误

我有这个菜单,但如果输入文本错误则向用户提供错误的选项不起作用。

if  [ -n "$1" -a -n "$2" -a -f "$1" ]
then

dts=($(cat $1 | cut -d: -f$2))

        for i in ${!dts[@]}
        do
                echo "$i) ${dts[$i]} "
        done

        read   -p "Select a user to delete: " OKSELUSRGRP
        p=$( echo $OKSELUSRGRP | grep -E ^[0-${#dts[@]}]$ )

                if [  -n "$p" ]
                then
                OKSELUSRGRP=${dts[$p]}
                else
                OKSELUSRGRP=""
                fi

else
        echo "Error, select a correct option"
        sleep 1
fi

有任何想法吗?

谢谢!

答案1

也许尝试这样的事情,虽然我不太确定你的脚本在做什么。

if  [ -n "$1" -a -n "$2" -a -f "$1" ]; then
    dts=($(cat $1 | cut -d: -f$2))
        for i in ${!dts[@]}; do
            echo "$i) ${dts[$i]} "
        done
        read -p "Select a user to delete: " OKSELUSRGRP
        p=$(echo $OKSELUSRGRP | grep -E ^[0-${#dts[@]}]$)
        while [ -z "$p" ]; do
            echo "Error, select a correct option"
            sleep 1
            read -p "Select a user to delete: " OKSELUSRGRP
            p=$(echo $OKSELUSRGRP | grep -E ^[0-${#dts[@]}]$)
        done
    OKSELUSRGRP=${dts[$p]}
else
        echo "
Usage: $0 [file] [something else]   

$0 can be used to select users or something.
"
fi

相关内容