我想将批处理脚本的第一个参数用作变量。如果它包含FULL,我会将所有变量设置为1,如果它是空的,我会要求每个变量。变量上的批处理脚本条件
但我不能让我的代码工作:
@echo off
color 02
::-----------------------------------------
:: Variables
set Preset=%1
set Profile=%2
set NeedToPassTheTests=1
set PublishAPI=0
REM ---------------------
REM We fill the variables depending on the arguments
echo(%1
if defined Preset (
if "%Preset%"=="FULL" (
echo FULL
set PublishOCPP=1
set NeedToPassTheTests=1
set PublishAPI=1
)
) else (
REM No preset so we are going to ask
set /p PublishAPI="Publish API ? (0 or 1).............. ? "
set /p NeedToPassTheTests="Test the projects before ? (0 or 1).............. ? "
)
echo NeedToPassTheTests %NeedToPassTheTests%
echo %PublishAPI%
echo.
pause
我得到
该命令的语法不正确
,也是我的回声在结束不打印的价值(当我删除我的ifs)它只显示回声关闭。
我以.bat文件开头,我试过的只是空测试(取自:What is the proper way to test if variable is empty in a batch file?),它也不起作用。
你知道为什么吗?
更新: 它工作时,我通过FULL作为参数,但我的变量没有更新。然而它的参数是空的,它不起作用(即不要求我填充变量)。
不要在*代码块中使用':: comment' *(括号中的语句序列) - 使用'rem'''是一个破碎的标签,代码块中的标签可能会导致意外的结果。 – Magoo
使用语法“if”%varname%“==”value“(dothis)else(dothat)'来比较可能包含分隔符(如空格)的字符串,否则会出现语法错误。引号将包含的值解释为单个字符串。 – Magoo
如果“%Preset%”==“”也给我一个错误的语法错误:( – user2088807