0

I keep getting this error:

./install.sh 
./install.sh: line 28: syntax error near unexpected token `do'
./install.sh: line 28: `                do'

When running this code (a work in progress):

# Set the prompt string:
PS3='Choose folder: ';

function choose() {

    # This works:
    #select folder;
    #do test -n "$folder" && echo You chose: $REPLY\) $folder && break;
    #echo 'Wrong entry, try again.';
    #continue;
    #done;

    select folder;
    do
        case $folder in
            'q')
                echo 'Exiting …';
                break;
                ;;
            *)
                do
                    test -n '$folder' && echo You chose: $REPLY\) $folder && break;
                    echo 'Wrong entry, try again.';
                    continue;
                done;
                ;;
        esac;
    done;

}

# Switch to glob folder location:
cd ~/Desktop;

# Create menu:
choose foo*;

# Do stuff with result of choose() here ...

# Exit program:
exit 0;

As you can probably see from above, I'm trying to create a menu that lets me choose from a list of globbed folders; unfortunately, I can't figure out why my nested do is creating the error I posted above.

Optimally, the user could choose from a list of directories; if they type q, then the program quits; if they make an invalid selection, the program makes them choose again until they pick a valid selection (or enter q to quit/exit).

I have yet to get the quitting part figured out completely, but before I can do that, I need to get past this do statement error.

Question: What is causing the syntax error near unexpected token `do' and how can I fix it?

6

Missing Loop Conditional

Your syntax is invalid. You may have other problems with your code as well, but this message is likely coming from the fact that you can't have a do keyword as if it were a block; you need to have one of the loop conditionals as well.

You currently have:

        *)
            do
                test -n '$folder' && echo You chose: $REPLY\) $folder && break;
                echo 'Wrong entry, try again.';
                continue;
            done;
            ;;

You are missing a for, while, or until keyword to start your loop.

1
  • 1
    Ahh, that makes sense now! Thank you for you help!!! You have put me on the right track. :) – mhulse Feb 24 '16 at 7:34
1

In general, when I get that type of error (unmatched if/else, do, while, etc) and I can't find the offending code, I make a backup of the script and start deleting chunks of code between any blocks until I just have a skeleton of control structures and that usually helps me find the missing keyword.

1
  • 1
    Awesome, thanks so much for the tip Mark! I'm pretty new to this level of bash scripting ... It's a little different than most languages I've used to date. Tips like this are very helpful as I really want to improve my bash scripting knowledge. Much appreciated. :) – mhulse Feb 25 '16 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.