1

If I try to run this script:

clear
echo -n "please enter a value"

read num 

if [ "$num" -eq 8 ] then
        echo "you entered 8"
else
        echo "the number you entered was not 8"

I get the following output/error:

please enter a value5
./script.sh: line 9: syntax error near unexpected token `else'
./script.sh: line 9: else'

Why wont this script run?

closed as off-topic by jasonwryan, jordanm, slm Apr 2 '15 at 0:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – jasonwryan, jordanm, slm
If this question can be reworded to fit the rules in the help center, please edit the question.

6

You're missing a semicolon or newline after the if clause, and there's no fi atthe end of the if block.

#!/bin/bash
clear
echo -n "please enter a value"

read num 

if [ "$num" -eq 8 ]
then
        echo "you entered 8"
else
        echo "the number you entered was not 8"
fi

Some other suggestions:

  • Scripts should always start with a #! line to tell the system which interpreter to use
  • Your comparison [ "$num" -eq 8 ] is a numeric comparison. If you're not sure that the user will actually enter a number, consider using a string comparison instead, [ 8 = "$num" ]
  • You can wrap the prompt into the read statement, read -p "Please enter a value: " num
  • @user4668401 All bash interpreters follow the same syntax. You can either do if condition; then on a single line, or if condition then on two separate lines. Some people prefer the single line approach; I prefer two. – roaima Apr 1 '15 at 23:00
  • thanks, after re-reading your answer - I realised what you meant with: "You're missing a semicolon or newline after the if clause" , thanks very helpful! – the_velour_fog Apr 1 '15 at 23:05

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