2010-10-01 132 views
2

我有這樣的運行作爲一個Shell腳本目標在我的Xcode項目「附近意外的標記‘elif的’語法錯誤」如何解決shell錯誤

# shell script goes here 
genstrings -u -a -o en.lproj *[hmc] */*[hmc] */*/*[hmc] 
if [ -f "$PROJECT_DIR/build/Release-macosx/UnicodeEscape" ] then 
    build/Release-macosx/UnicodeEscape "en.lproj/Localizable.strings" 
elif [ -f "$PROJECT_DIR/build/Debug-macosx/UnicodeEscape" ] then 
    build/Debug-macosx/UnicodeEscape "en.lproj/Localizable.strings" 
fi 

exit 0 

我得到這個錯誤:

/Users/aa/Dropbox/Developer/Pandamonia LLC/iPhone/Acey Deucey/build/Acey Deucey.build/Release/GenerateLocalizedStrings.build/Script-00F66869125625D9009F14DA.sh: line 7: syntax error near unexpected token elif' /Users/aa/Dropbox/Developer/Pandamonia LLC/iPhone/Acey Deucey/build/Acey Deucey.build/Release/GenerateLocalizedStrings.build/Script-00F66869125625D9009F14DA.sh: line 7: elif [ -f "$PROJECT_DIR/build/Debug-macosx/UnicodeEscape" ] then' Command /bin/sh failed with exit code 2

回答

5
所有的

首先,不要將其標記爲bashsh,你有一個外殼,類型echo $SHELL知道哪個殼您使用,或把家當在腳本的開始(#!/usr/bin/env bash

在命令後面加上分號,包括[ ... ]這是test的別名。命令終止符是換行符,;,&&,||&並且是強制性的。您可以在ifthen之間放置幾個命令,因此這些分號是強制性的。

if [ -f "$PROJECT_DIR/build/Release-macosx/UnicodeEscape" ] ; then 
    build/Release-macosx/UnicodeEscape "en.lproj/Localizable.strings" ; 
elif [ -f "$PROJECT_DIR/build/Debug-macosx/UnicodeEscape" ] ; then 
    build/Debug-macosx/UnicodeEscape "en.lproj/Localizable.strings" ; 
fi 
+0

的','在聲明的結尾是無用的空語句。 – Jens 2012-05-08 10:18:41

+0

@Jens:除非你不換線。 – Benoit 2012-05-08 13:58:50

+1

我不明白你想說什麼。你能改說嗎? – Jens 2012-05-08 14:04:05

4

then聲明必須在一個新行,或如果條件與;分開。

3

在關鍵字then之前需要分號。

# shell script goes here 
genstrings -u -a -o en.lproj *[hmc] */*[hmc] */*/*[hmc] 
if [ -f "$PROJECT_DIR/build/Release-macosx/UnicodeEscape" ]; then 
    build/Release-macosx/UnicodeEscape "en.lproj/Localizable.strings" 
elif [ -f "$PROJECT_DIR/build/Debug-macosx/UnicodeEscape" ]; then 
    build/Debug-macosx/UnicodeEscape "en.lproj/Localizable.strings" 
fi 

exit 0 
0

對我來說,這個問題竟然是不正確的行結束這應該是LF,而不是CRLF
發生這種情況是因爲我從Windows開始工作。

您可以通過在記事本++檢查:

View >Show symbol >Show all characters

和修復:

Edit >EOL Conversion >UNIX/OSX Format