Bash确认输入或重新输入

时间:2017-08-18 12:21:35

标签: bash prompt

我有一个非常简单的BASH脚本,询问用户是否想要输入一些输入。回复是yn

如果他们输入n脚本退出。

如果他们输入y,他们会被问到一些问题。最后,我想请他们确认输入。

如果它是正确的我们会继续,如果不是,我想让他们回到问题重新输入信息。

到目前为止,我已经:

while true; do
    echo -e "Enter details ? "
    read yn
    case $yn in
        [Nn]* ) exit;;
        [Yy]* ) 
            echo -e "description"
            read desc

            echo -e "Address"
            read address

        * ) echo "Please answer yes or no.";;
    esac
done

这似乎工作正常,所以我试图添加确认..但那不起作用。

while true; do
    echo -e "Enter details ? "
    read yn
    case $yn in
        [Nn]* ) exit;;
        [Yy]* ) 
            echo -e "description"
            read desc

            echo -e "Address"
            read address

            echo -e "$desc - $address"

            while true; do
                echo -e "\nAre these details correct ? "
                read conf
                case $conf in
                    [Nn]* ) // return to enter details //
                    [Yy]* ) // carry on with the details entered // break;;
                esac
            done


        * ) echo "Please answer yes or no.";;
    esac
done

目标如下

用户选择y并输入说明和地址。该页面向他们展示了他们输入的内容的摘要,并询问他们are these details correct ?他们是否继续前进并且脚本进展。输入错误,然后脚本返回它们以输入描述,然后输入地址。

有人可以建议如何做到这一点。 感谢

3 个答案:

答案 0 :(得分:1)

您可以添加一个循环级别并使用break [n]的可选级别指示符:

while true; do
    echo -e "Enter details ? "
    read yn
    case $yn in
        [Nn]*) exit;;
        [Yy]*)
            while true; do
                echo -e "description"
                read desc

                echo -e "Address"
                read address

                echo -e "$desc - $address"

                while true; do
                    echo -e "\nAre these details correct ? "
                    read conf
                    case $conf in
                        [Nn]* ) break 1;;
                        [Yy]* ) break 3;;
                    esac
                done
            done;;
        *) echo "Please answer yes or no.";;
    esac
done

答案 1 :(得分:0)

请勿使用case。 以脚本为例:

#!/bin/bash

while true; do
    echo -e "Enter details ?(y/n)"
    read yn
    if [[ $yn == "y" ]];then
            echo -e "description"
            read desc

            echo -e "Address"
            read address

            echo -e "$desc - $address"

            while true; do
                echo -e "\nAre these details correct ? (y/n)"
                read conf
                if [[ $conf == "y" ]];then
                    echo "All done"
                    break
                elif [[ $conf == "n" ]];then
                    break
                else
                    continue
                fi
            done
    elif [[ $yn == "n" ]];then
        break
    else
        continue
    fi

答案 2 :(得分:0)

你去了:

DONE=""
while [ "$DONE" != "true" ]; do
    echo -e "Enter details ? "
    read yn
    case $yn in
        [Nn]* ) exit;;
        [Yy]* )
            echo -e "description"
            read desc

            echo -e "Address"
            read address

            echo -e "$desc - $address"

            while true; do
                echo -e "\nAre these details correct ? "
                read conf
                case $conf in
                    [Nn]* )
                        break;;
                    [Yy]* )
                        echo "doing something with your values"
                        DONE="true"
                        break;;
                esac
            done
        ;;


        * ) echo "Please answer yes or no.";;
    esac
done