0

I want to modify an existing bash script to avoid asking input data from the usedr if any parameter is passed. now the part if the existing script is as follow and works fine :

options=("a" "b" "c"  "d"  "e")
select opt in "${options[@]}"

I'm changing with this:

options=("a" "b"  "c"  "d")
if [ $# = 0 ] ; then
        select opt in "${options[@]}"
else
        opt=$1
fi

When i run it (with or without parameter) i get the following error

/usr/local/sbin/script.sh: line 486: syntax error near unexpected token `else'
/usr/local/sbin/script.sh: line 486: `else'

Line 486 is the line with my else statement Any idea of what I'm doing wrong?

3
  • 1
    Your select command is incomplete. See help select Commented Dec 1, 2015 at 9:52
  • ok got it, I left the "do" part outside the if statement which was few lines below Commented Dec 1, 2015 at 10:01
  • @Cris It's not related to if statement. select needs a list command(s). Probably "it works" without if because there's some other commands after select. Look for done after select and you'll find it if it works. Commented Dec 1, 2015 at 10:01

1 Answer 1

2

The usage of select is not correct. It's in the form:

    select opt in "${options[@]}"
    do 
       echo "You selected ${opt}"
        ....
    done
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, indeed the do part was few lines below and i missed it, need drink coffee before edit anything else :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.