程序第二次不进入第二个循环

时间:2019-06-02 20:30:39

标签: bash shell sh

我正在创建自己的bash脚本,但是当我离开第二个菜单时,代码不允许我再次进入第二个菜单。

我不得不解释此错误的唯一方法是,当我将6放回到第一个位置,而读取修复了6个错误时,是什么引起了这个问题的困惑呢? 但这没有意义,因为如果固定了6,它将回显“ <--- back”

有人可以告诉我为什么吗?

  while [ "$myscript" != "6" ]
   do
    echo "Script Menu"
    echo " 1 - Open"
    echo " 2 - Download"            
    echo " 3 - Update && Upgrade"   
    echo " 6 - Exit"

    echo "Choose  Your Script: "
    read myscript

    case $myscript in
            1)
                while [ "$open" != "6" ]
                    do
                        clear
                        echo "Open Menu"
                        echo " 1 - Rythombox"
                        echo " 2 - Anaconda"
                        echo " 3 - VSCode"
                        echo " 4 - Terminal"
                        echo " 6 - <-----Back"

                        echo "Choose Your Open: "
                        read open 

                        case $open in
                            1) 
                                echo "  Opening Rhythmbox"
                                gnome-terminal --tab -- "rhythmbox"
                            ;;
                            6) 
                                echo "<--- Back"
                            ;;

                            *)
                               echo "Not a option!"
                            ;;

                        esac
          done

1 个答案:

答案 0 :(得分:1)

正如戈登·戴维森(Gordon Davisson)所注意到的那样,问题确实是 $open自上次起仍为“ 6”
解决方案只是简单地以合理的顺序读取和比较输入选择,而不是在读取变量之前测试[ "$open" != "6" ]

while   echo "Script Menu"
        echo " 1 - Open"
        echo " 2 - Download"            
        echo " 3 - Update && Upgrade"   
        echo " 6 - Exit"
        echo "Choose  Your Script: "
        read myscript
        [ "$myscript" != "6" ]
do
    case $myscript in
    1)  while   clear
                echo "Open Menu"
                echo " 1 - Rythombox"
                echo " 2 - Anaconda"
                echo " 3 - VSCode"
                echo " 4 - Terminal"
                echo " 6 - <-----Back"
                echo "Choose Your Open: "
                read open 
                [ "$open" != "6" ]
        do
            case $open in
            1)  echo "  Opening Rhythmbox"
                gnome-terminal --tab -- "rhythmbox"
                ;;
            6)  echo "<--- Back"
                ;;
            *)  echo "Not a option!"
                ;;
            esac
        done
    esac
done