语法错误 Cygwin If Else 条件

Syntax error Cygwin If Else condition

我正在使用 Cygwin 运行 脚本,当我使用 if else 条件时出现语法错误。 你能告诉我我的脚本有什么问题吗?

./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

谢谢

使用 shellcheck.net 这样的东西。您的 if.

有语法错误

正确的代码-

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

您的 if 陈述不正确:

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

注意 then 语句和 $first 变量的双引号。