Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This question already has an answer here:

I've been tinkering with Linux and Unix for years but still a novice in my mind and recently find myself trying to be more pro with it as I work in IT. So with that notion I'm studying shell scripting.

I've hit a snag in ubuntu using the latest version 13.10 Saucy. When I use the select command in a sh script it doesn't work, depending on how I format the command it will either return Unexpected "do" or Unexpected "done". See the following two examples:

This section of code produces an unexpected "do" error:

#/bin/bash
PS3='Please enter your choice'
select opt in option1 option2 option3 quit
do
     case $opt in
       "option1")
            echo "you chose choice 1" ;;
        "option2")
            echo "you chose choice 2" ;;
        "option3")
            echo "you chose choice 3" ;;
        "quit")
            break ;;
        *) echo invalid option ;;
    esac
done

This section of code produces an unexpected "done" error.

#/bin/bash
PS3='Please enter your choice'
select opt in option1 option2 option3 quit ; do
     case $opt in
       "Option1")
            echo "you chose choice 1" ;;
        "Option2")
            echo "you chose choice 2" ;;
        "Option3")
            echo "you chose choice 3" ;;
        "quit")
            break ;;
        *) echo invalid option ;;
    esac
done

When I enter these parameters into the command line interactively or manually I get the desired result which is a list of choices to choose from. However when executed from a script I get the before mentioned errors. Also a side note I have tried this in Fedora as a script and it worked perfectly! Is there something I need to do differently or a setting in a configuration file I need to check? Am I the only one having this issue?

share|improve this question

marked as duplicate by belacqua, Radu Rădeanu, LiveWireBT, karel, Warren Hill Oct 28 '13 at 9:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

up vote 1 down vote accepted

The Syntax error: "do" unexpected or Syntax error: "done" unexpected errors are almost certainly because you are trying to run your script by typing

sh yourscript

which overrides the #!/bin/bash directive and forces the script to be run in the /bin/sh interpreter - which by default is a symlink to /bin/dash, which doesn't support the select syntax. You should just make the script executable

chmod +x yourscript

and then invoke it as

./yourscript

The other obvious problem is that your option list uses lower case option1 option2 option3 but your case statement tests for Option1 Option2 Option3 - it should be

#/bin/bash
PS3='Please enter your choice '
select opt in option1 option2 option3 quit; do
     case "$opt" in
       "option1")
            echo "you chose choice 1" ;;
       "option2")
            echo "you chose choice 2" ;;
       "option3")
            echo "you chose choice 3" ;;
       "quit")
            break ;;
       *) echo invalid option ;;
    esac
done

share|improve this answer
    
You are correct! Though the capped "O" in options was just a type-o on the 2nd section of example code I copied and didn't correct to fit my adjusted statement. –  Keith Gregory Oct 27 '13 at 2:42

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