Windows Batch如果阻止语法

时间:2013-04-26 16:03:47

标签: windows file batch-file

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

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!"

2 个答案:

答案 0 :(得分:7)

'在自己的行上启动每个IF,在左括号前留一个空格。

以上解决问题的原因是.cmd文件每行需要一个命令,而右括号“结束”该语句。因此,在右括号后面的附加IF在同一行上开始另一个意外的命令。

构造

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

被有效地解释为一行,右括号和回车符结束行和'if'。

这使得一个“棘手的”'if'构造

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

由于'else'是'if'语法的一部分,'else'必须与右括号包含在同一行,以“继续”'if'语句。

在左括号之前需要空格作为简单的语法要求。

答案 1 :(得分: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!"