2016-12-07 35 views
0

我使用Cygwin來運行腳本,當我使用if else條件時出現語法錯誤。 你能告訴我我的腳本有什麼問題嗎?語法錯誤Cygwin如果否則條件

./test.sh:第10行:附近意外的標記else' ./test.sh: line 10:別的語法錯誤'

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

感謝

+1

你錯過了'then'。 '['和'$ first'之間的空格。我可以建議使用http://www.shellcheck.net嗎? – Biffen

回答

1

if說法是不正確:

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

注意then聲明,爲$first可變雙引號。

+0

謝謝:)它的工作原理 –

0

像這樣的東西用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