1

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
LinenG
  • 109
  • 3
  • 14
  • Take a look at: http://www.shellcheck.net/ – Cyrus Jul 06 '15 at 20:03
  • Thanks, actually I've been there, though it suggests "Did you forget to move the ;; after extending this case item?" - I don't think it's right or maybe I can't see it... – LinenG Jul 06 '15 at 20:25
  • Your code is inscrutable. I smell an XY problem: forget about your code and tell us what are you trying to do. – glenn jackman Jul 06 '15 at 20:44
  • The idea is to run a user through the a number of questions (depending on answer go into innerloop with another question) - by catching the answers use them as variables to do some action, I'm stuck within the second inner loop. After I'm breaking from it I'd like to continue the script with few more questions. I hope I managed to explain somehow properly... – LinenG Jul 06 '15 at 20:54

1 Answers1

2

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

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Okay, it means I'm asking to much from bash.Thanks! – LinenG Jul 08 '15 at 10:20
  • I'm not aware of any language that would allow this with `break`. The behavior you want is essentially provided by `goto`; you would just write empty loops to serve as targets, unless I'm overestimating the generality of what you want. – chepner Jul 08 '15 at 23:47
  • 2
    `continue 2` in place of `break 3` would simply restart the loop enclosing the current while loop, not jump to any specific inner loop. – chepner Jan 19 '16 at 21:48