PyPunk
PyPunk

Reputation: 79

syntax error near unexpected token value in bash

I'm new to bash and I'm trying to write a script that will prompt the user to select a number, and open the corresponding file. Right now i just wanted to get the options to work properly but I get the error:

./filescript: line 7: syntax error near unexpected token `elif'
./filescript: line 7: `elif [[$server == 2]]'

Anytime I try to make a selection when it's ran. Here's what I have, any advice is appreciated!

#!/bin/bash

echo "Which file would you like to open: "
read input
if [[$input == 1]] then
    echo "This is the first option"
elif [[$input == 2]]
    echo "This is the second option"
else
    echo "Error"
fi

Upvotes: 0

Views: 8315

Answers (2)

Zuko
Zuko

Reputation: 367

There're syntax error in your code.

  • before then , ; should added
  • in the test brace , must have space around test condition
  • you forgot then keyword in second elif

the correction will be

#!/bin/bash

echo "Which file would you like to open: "
read input
if [[ $input == 1 ]] ;then
    echo "This is the first option"
elif [[ $input == 2 ]] ;then
    echo "This is the second option"
else
    echo "Error"
fi

See online version http://tpcg.io/8zfbh4

Upvotes: 1

Jose Martinez
Jose Martinez

Reputation: 12022

I typically do my IF statements like this for strings

if [ "$input" == "1" ]

or like this for numerical

if [ $input -eq 1 ]

EDIT: try putting a space after the [ and before the ]

$ if [[ $X == 1 ]] ; then echo "yes"; fi
yes

$ if [[$X == 1]] ; then echo "yes"; fi
bash: [[1: command not found

Upvotes: 1

Related Questions