Alexsander Akers
Alexsander Akers

Reputation: 16024

How can I fix shell error "syntax error near unexpected token 'elif'"

I have this run as a Shell Script target in my Xcode project

# 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

I get this error:

/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

Upvotes: 2

Views: 12617

Answers (4)

Ujjwal Singh
Ujjwal Singh

Reputation: 4988

For me the issue turned out to be incorrect line ending which should be LF and not CRLF.
This happened because I was working from Windows.

You can check this in Notepad++ by:

View > Show symbol > Show all characters

and fix by:

Edit > EOL Conversion > UNIX/OSX Format

Upvotes: 0

Benoit
Benoit

Reputation: 79185

First of all, do not tag it bash and sh, you have one shell, type echo $SHELL to know which shell you use, or put a shebang at the start of your script (#!/usr/bin/env bash)

put semicolons after your commands, including [ ... ] which is an alias for test. Command terminators are newline, ;, &&, || and & and are mandatory. You can put several commands between if and then, so those semicolons are mandatory.

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

Upvotes: 5

Yuji
Yuji

Reputation: 34185

You need semicolons before the keyword 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

Upvotes: 3

Douglas Leeder
Douglas Leeder

Reputation: 53310

The then statement needs to be on a new line, or separate from the if condition with ;.

Upvotes: 4

Related Questions