Bash:循环中有两个循环

时间:2017-06-02 15:53:28

标签: bash loops while-loop

作为初学者,我不确定这是否是进行多个循环的最佳方法!虽然它运作良好,请建议我是否有一种优雅的方式(我不仅限于bash,但我不熟悉其他语言)

#!/bin/bash
while :; do
            read -n1 -e -p"Top Levels 1-5: " top_levels
            if [ "$top_levels" == "1" ]; then
                echo "top level 1"
            elif [ "$top_levels" == "2" ]; then
                while :; do
                            read -n1 -e -p"Sub Levels 1-5: " sub_levels
                            if [ "$sub_levels" == "1" ]; then
                                echo "sub level 1"
                            elif [ "$sub_levels" == "2" ]; then
                                echo "sub level 2"
                            elif [ "$sub_levels" == "3" ]; then
                                while :; do
                                            read -n1 -e -p"Final Levels 1-5: " final_levels
                                            if [ "$final_levels" == "1" ]; then
                                                echo "Final level 1"
                                            elif [ "$final_levels" == "2" ]; then
                                                echo "Final level 2"
                                            elif [ "$final_levels" == "3" ]; then
                                                echo "Final level 3"
                                            elif [ "$final_levels" == "4" ]; then
                                                echo "Final level 4"
                                            else
                                                echo "bye"
                                                break
                                            fi
                                        done
                            elif [ "$sub_levels" == "4" ]; then
                                echo "sub level 4"
                            else
                                echo "bye"
                                break
                            fi
                        done
            elif [ "$top_levels" == "3" ]; then
                echo "top level 3"
            elif [ "$top_levels" == "4" ]; then
                echo "top level 4"
            else
                echo "bye"
                exit
            fi
        done

1 个答案:

答案 0 :(得分:0)

我这样写

#!/bin/bash

process_final_levels() {
    while :; do
        read -n1 -e -p"### Final Levels 1-5: " final_levels
        case $final_levels in
          1) echo "Final level 1";;
          2) echo "Final level 2";;
          3) echo "Final level 3";;
          4) echo "Final level 4";;
          *) echo "returning to sub levels"
            break;;
        esac
    done
}


process_sub_levels() {
    while :; do
        read -n1 -e -p"# Sub Levels 1-5: " sub_levels

        case "$sub_levels" in
          1) echo "sub level 1" ;;
          2) echo "sub level 2" ;;
          3) process_final_levels;;
          4) echo "sub level 4" ;;
          *) echo "returning to top levels"
             break;;
        esac
    done
}


while :; do
    # get top levels to work with
    read -n1 -e -p"Top Levels 1-5: " top_levels

    case "$top_levels" in
      1) echo "top level 1" ;;
      2) process_sub_levels ;;
      3) echo "top level 3" ;;
      4) echo "top level 4" ;;
      *) echo "Bye"
         exit;;
    esac
done

我更改了提示符并退出字符串,以便更直观地了解您正在处理的级别。我没有添加评论,b / c我不知道这是为了什么。我功能化了,b / c任何需要修改,测试或维护的代码都应该被功能化。