Select (loop) or command not working in shell-script

Posted by user208098 on Ask Ubuntu See other posts from Ask Ubuntu or by user208098
Published on 2013-10-26T22:08:58Z Indexed on 2013/10/26 22:14 UTC
Read the original article Hit count: 273

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 so my question is why isn't it working in Ubuntu, is this a difference between RHL and Debian? Or is it a bug in the latest version of Ubuntu?

Thanks in advance for any help! KG

© Ask Ubuntu or respective owner

Related posts about command-line

Related posts about 13.10