0

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?

0

1 Answer 1

3

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

1
  • 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. Oct 27, 2013 at 2:42

Not the answer you're looking for? Browse other questions tagged .