0

I am using Cygwin to run a script, I am getting syntax error when i use if else condition. Can you please tell me what is wrong with my script ?

./test.sh: line 10: syntax error near unexpected token else' ./test.sh: line 10:else '

#!/bin/bash
date
schema=''
table='JJJJ'
first=${table:0:1}
echo $first
if [$first == 'J'] 
echo 'SUCCESS';
else 
echo 'error';
fi

thanks

Houssem Hariz
  • 55
  • 3
  • 9
  • 1
    You're missing a `then`. And a space between `[` and `$first`. May I suggest using http://www.shellcheck.net? – Biffen Dec 07 '16 at 11:20

2 Answers2

2

Your if statement is incorrect:

if [ "$first" == 'J' ]; then
  echo 'SUCCESS';
else 
  echo 'error';
fi

Note the then statement and the double quote for the $first variable.

oliv
  • 11,216
  • 19
  • 37
1

Use shellcheck.net for stuff like this. You have syntax errors around your if.

Correct code-

#!/bin/bash
date
schema=''
table='JJJJ'
first=${table:0:1}
echo $first
if [ $first == 'J' ] 
then
echo 'SUCCESS';
else 
echo 'error';
fi
Chem-man17
  • 1,480
  • 10
  • 25