1

我真的找不到这个脚本的语法有什么问题

if 块有问题,但我看不出问题出在哪里。

@ECHO off

set PROJECT=C:/ws/UI/Trunky/plugins/.
set EXE=grails
set MVN=mvn
set DEP=--refresh-dependencies
set CLEAN=clean
set ASSET=ProcessAssets -Dgrails.env=production -DRESOURCE_PATH=./assets
set TEST=test-app unit: --non-interactive
set FIXJS=-s ./hudson-config/settings.xml package
set INPUT="%1"

IF "%1" == "-dep"(
    ECHO %EXE% %DEP%
    ECHO "Refreshing dependencies."
) IF "%1" == "-clean"(
    ECHO %EXE% %CLEAN%
    ECHO "Cleaning up."
)IF "%1" == "-asset"(
    ECHO %EXE% %ASSET%
    ECHO "Processing assets."
)IF "%1" == "-test"(
    ECHO %EXE% %TEST%
    ECHO "Running Groovy tests."
)IF "%1" == "-fixjs"(
    ECHO %MVN% %FIXJS%
    ECHO "Copying JS, Running Jasmine and performing coverage report."
)IF "%1" == "-lazy"(
     ECHO rmdir /s /q "%PROJECT%"
     ECHO %EXE% %DEP%
     ECHO %EXE% %CLEAN%
     ECHO %EXE% %ASSET%
     ECHO %MVN% %FIXJS%
)

ECHO "Done. Have a great day!"
4

2 回答 2

7

'Start each IF on its own line, leave a space before the opening parenthesis.

The reason that the above fixes the problem is that .cmd files expect one command per line, and the closing parenthesis "ends" the statement. Thus, the additional IF following the closing parenthesis begins another, unexpected, command on the same line.

The construct

if "%1" == "somestring" (
echo "%1"
)

is effectively interpreted as one line, with the closing parenthesis and carriage return ending the line and the 'if'.

This makes for one "tricky" 'if' construct

if "%1" == "somestring" (
echo "%1"
) else (
echo "Not %1"
)

Since 'else' is part of the 'if' syntax the 'else' must be included on the same line as the closing parenthesis to "continue" the 'if' statement.

The space is required before the opening parenthesis as a simple syntax requirement.

于 2013-04-26T16:07:12.227 回答
0

上面的答案是正确的 - 这是一个使用您的代码的可视化示例。

@ECHO off

set PROJECT=C:/ws/UI/Trunky/plugins/.
set EXE=grails
set MVN=mvn
set DEP=--refresh-dependencies
set CLEAN=clean
set ASSET=ProcessAssets -Dgrails.env=production -DRESOURCE_PATH=./assets
set TEST=test-app unit: --non-interactive
set FIXJS=-s ./hudson-config/settings.xml package
set INPUT="%1"

IF "%1" == "-dep" (
    ECHO %EXE% %DEP%
    ECHO "Refreshing dependencies."
) 

IF "%1" == "-clean" (
    ECHO %EXE% %CLEAN%
    ECHO "Cleaning up."
)

IF "%1" == "-asset" (
    ECHO %EXE% %ASSET%
    ECHO "Processing assets."
)

IF "%1" == "-test" (
    ECHO %EXE% %TEST%
    ECHO "Running Groovy tests."
)

IF "%1" == "-fixjs" (
    ECHO %MVN% %FIXJS%
    ECHO "Copying JS, Running Jasmine and performing coverage report."
)

IF "%1" == "-lazy" (
     ECHO rmdir /s /q "%PROJECT%"
     ECHO %EXE% %DEP%
     ECHO %EXE% %CLEAN%
     ECHO %EXE% %ASSET%
     ECHO %MVN% %FIXJS%
)

ECHO "Done. Have a great day!"
于 2013-04-27T04:47:57.387 回答