bashwhile-loopcasenested-loopsinteractive-shell

How to continue in nested loops (after 2nd innerloop) in BASH?


Would like kindly to ask for you advice.

I'm building interactive bash script with nested loops using while true, read and case. My problem is that after second innerloop (when choosing 'loop 1.3') I'd like to continue script with another inner loop, but it fails (part with my comment about failure).

I assume I'm missing something in syntax, but can't find it out...

Below is the code of the script. Thanks in advance.

#!/bin/bash
while true; do
read -rep $'Loop #1 (outerloop)? \n\tYES\tNO\n\n' yn
case $yn in
    [Yy]* )
    while true; do
    read -rep $'Loop #1.2 (first inner loop)? Please, choose variant 1 or 2.\n\t1\t2\n\n' yn
    case $yn in
    [1]* )
            while true; do
            read -rep $'Loop #1.3 (second inner loop)?\n\tYES\tNO\n\n' yn
            case $yn in
            [Yy]* ) read -rep $'Loop #1.3. Variant Yes.:\n\n' LOOP3Q1; break 3;;
            [Nn]* ) break 3;;
            esac
            done; break 3;;
    [2]* ) 
            while true; do
            read -rep $'Loop #1.3 (second inner loop).\n\tYES\tNO\n\n' yn
            case $yn in
            [Yy]* ) read -rep $'Loop #1.3 variant Yes.\n\n' LOOP3Q2; break 3;;
            [Nn]* ) break 3;;
            esac
            done; break 3;;
# Here it failes. If here I break all loops - it works, but I need to continue...
            echo "Continue loop 1.2 (first inner loop)"
            while true; do
            read -rep $'Loop #1.3 (second inner loop).\n\tYES\tNO\n\n' yn
            case $yn in
            [Yy]* ) read -rep $'Loop #1.3 variant Yes.\n\n' LOOP3Q2; break 3;;
            [Nn]* ) break 3;;
            esac
            done; break 3;
    esac
    done; break 2;;
    Nn]* ) break;;
 esac
 done

Solution

  • You can't jump into an arbitrary loop with break; the argument specifies how many loops in the "stack" to break out of.