Search code examples
bashshellnested-loops

Nested while loop in bash script - inner being skipped


I tried reading up on this, but am unable to find anything fixing my issue.. Issue is: I have a nested while loop and the inner is being skipped completely. The outter loop runs fine and the inner code does work as expected when taken out and run on its own. I'm new to this, so just looking for a straight forward way to fix, not necessarily a way to re-write everything (unless necessary) haha. I omitted a few things replacing with generic [command], etc.

guid=0
procedure=0
olcmd="${userbin}/[command] ${guid}"

guidver=true;
while $guidver; do
    guidver=false;
    read -p "
Input log GUID below and press 'Enter'?
"   guid
    if [[ $guid == ????????-????-????-????-???????????? ]]; then
        printf "Good GUID, continuing\n";
    else echo "Bad value, please use only Index GUIDs."; guidver=true
    olver=true;
    while $olver; do
        olver=false;
        if $olcmd | grep -i '[text]'>/dev/null; then
        printf "\n[text]\n\n"
            continue
#       guidver=false;
        else printf "\n[text]\n\n"
        olver=true;
        break
        fi
    done
    fi
done

I really appreciate any help!


Solution

  • Your indentation is misleading:

    Check your code with proper indentation

    guid=0
    procedure=0
    olcmd="${userbin}/[command] ${guid}"
    
    guidver=true;
    while $guidver; do
        guidver=false;
        read -p "
        Input log GUID below and press 'Enter'?
        "   guid
        if [[ $guid == ????????-????-????-????-???????????? ]]; then
            printf "Good GUID, continuing\n";
        else echo "Bad value, please use only Index GUIDs."; 
            guidver=true
            olver=true;
            while $olver; do
                olver=false;
                if $olcmd | grep -i '[text]'>/dev/null; then
                    printf "\n[text]\n\n"
                        continue
                    #       guidver=false;
                else printf "\n[text]\n\n"
                    olver=true;
                    break
                fi
            done
        fi
    done
    

    See? Your second while is inside the else branch of your first if statement

    I think, what you meant would be

    guid=0
    procedure=0
    olcmd="${userbin}/[command] ${guid}"
    
    guidver=true;
    while $guidver; do
        guidver=false;
        read -p "
        Input log GUID below and press 'Enter'?
        "   guid
        if [[ $guid == ????????-????-????-????-???????????? ]]; then
            printf "Good GUID, continuing\n";
        else echo "Bad value, please use only Index GUIDs."; 
            guidver=true
        fi
    
        olver=true;
        while $olver; do
            olver=false;
            if $olcmd | grep -i '[text]'>/dev/null; then
                printf "\n[text]\n\n"
                    continue
                #       guidver=false;
            else printf "\n[text]\n\n"
                olver=true;
                break
            fi
        done
    done