bash 虽然?循环选择菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4180137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:54:37  来源:igfitidea点击:

bash While? loop choice menu

bash

提问by jmituzas

#!/bin/bash
echo "Please enter your Host Name"
read hname
echo "You have entered $hname, is this correct?"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) sh runscript.sh; break;;
         No ) ;
    esac
done

How can I make this work so that if answer is incorrect go back to ask it again? I know I need a whileloop to achieve this but don't know how to write it up.

我该如何进行这项工作,以便如果答案不正确,请返回再次提问?我知道我需要一个while循环来实现这一点,但不知道如何写出来。

回答by jcomeau_ictx

This ought to do it:

这应该这样做:


while [ "$yn" != "Yes" ]; do
 echo "Please enter your Host Name"
 read hname
 echo "You have entered $hname, is this correct? (Yes or No)"
  read yn
done
sh runscript.sh

回答by jmituzas

Here's another one you helped me out on writing:

这是你帮助我写作的另一篇文章:

#!/bin/bash

        while ["$yn" != "Yes" ]; do

    echo "Choose your type of installation"

    echo "1) Home Media Server"

    echo "2) Home Communication Server"

    echo "3) Enterprise Communication Server"

    echo "4) Hosting Server"

    echo "5) Individual Packages"

    echo "6) exit"

    read case;



    #simple case bash structure

    # note in this case $case is variable and does not have to

    # be named case this is just an example



    case $case in

        1) echo "You have entered Home Media Server, is this correct? (Yes or No)"

      read yn

                echo "Running script for Home Media Server";;



        2) echo "You have entered Home Communication Server, is this correct? (Yes or No)"

      read yn

                echo "Running script for Home Communication Server";;



        3) echo "You have entered Enterprise Communication Server, is this correct? (Yes or No)"

      read yn

                echo "Running script for Enterprise Communication Server";;



        4) echo "You have entered Hosting Server, is this correct? (Yes or No)"

      read yn

                echo "Running script for Hosting Server";;



        5) echo "You have entered $hname, is this correct? (Yes or No)"

      read yn

                echo "Running script for Individual Packages";;



        6) exit

    esac 

Thanks once again.

再次感谢。