Batch file Windows批处理脚本:为什么此IF命令的语法不正确? 描述
如果文件Batch file Windows批处理脚本:为什么此IF命令的语法不正确? 描述,batch-file,cmd,windows-10,Batch File,Cmd,Windows 10,如果文件Init.marker比使用Windows批处理脚本()的文件setup.py和setup.cfg更新,则尝试将Init打印到屏幕上时: 我遇到一个命令语法不正确。错误 错误从何而来 假设我们将上述批处理脚本写入名为make_init.cmd的文件,该文件与文件init.marker,setup.py,setup.cfg位于同一目录中。 (我们可以使用批处理脚本创建这些文件,请参见[1]。有关如何使用批处理脚本创建空文件的详细信息,请查看。)让我们使用cmd.exe运行它,它将提供以下
Init.marker
比使用Windows批处理脚本()的文件setup.py
和setup.cfg
更新,则尝试将Init打印到屏幕上时:
我遇到一个命令语法不正确。
错误
错误从何而来
假设我们将上述批处理脚本写入名为make_init.cmd
的文件,该文件与文件init.marker
,setup.py
,setup.cfg
位于同一目录中。
(我们可以使用批处理脚本创建这些文件,请参见[1]。有关如何使用批处理脚本创建空文件的详细信息,请查看。)让我们使用cmd.exe运行它,它将提供以下输出:
User> make_init.cmd
User> FOR /F %i IN ('DIR /B /O:D init.marker setup.py setup.cfg') DO SET last_modified_file=%i
User> SET last_modified_file=init.marker
User> SET last_modified_file=setup.py
User> SET last_modified_file=setup.cfg
The syntax of the command is incorrect.
User> IF "setup.cfg"=="init.marker" (
首先让我们看看FOR语句是否按照预期运行,方法是运行相同的脚本,但将if语句替换为简单的ECHO语句。(见[2])
输出是我们所期望的:
User> make_init.cmd
User> COPY /B NUL 1>init.marker
User> COPY /B NUL 1>setup.py
User> COPY /B NUL 1>setup.cfg
User> FOR /F %i IN ('DIR /B /O:D init.marker setup.py setup.cfg') DO SET last_modified_file=%i
User> SET last_modified_file=init.marker
User> SET last_modified_file=setup.py
User> SET last_modified_file=setup.cfg
User> ECHO setup.cfg
setup.cfg
据我所知,这意味着所提到的不正确命令必须是
如果“%last\u modified\u file%”=“init.marker”(
其计算结果为如果“setup.cfg”=“init.marker”(
那么,为什么在任何其他情况下(见[3])这样的IF语句是完全有效的呢
问题:
上面的IF命令的语法有什么问题
附件
[1] 创建所需文件的脚本扩展名
COPY /B NUL > init.marker
COPY /B NUL > setup.py
COPY /B NUL > setup.cfg
FOR /F %%i IN ('DIR /B /O:D init.marker setup.py setup.cfg') DO SET last_modified_file=%%i
IF "%last_modified_file%"=="init.marker"(
ECHO Init has already been made
)
[2] 用ECHO语句替换IF语句
COPY /B NUL > init.marker
COPY /B NUL > setup.py
COPY /B NUL > setup.cfg
FOR /F %%i IN ('DIR /B /O:D init.marker setup.py setup.cfg') DO SET last_modified_file=%%i
ECHO %last_modified_file%
:: Filename: test_if.cmd
:: How to execute: test_if.cmd hello
IF "%1"=="hello" (
SET the_greeting_message=Greetings, traveller.
)
IF "%the_greeting_message%"=="Greetings, traveller." (
ECHO %the_greeting_message%
)
[3] IF语句的简单测试脚本
COPY /B NUL > init.marker
COPY /B NUL > setup.py
COPY /B NUL > setup.cfg
FOR /F %%i IN ('DIR /B /O:D init.marker setup.py setup.cfg') DO SET last_modified_file=%%i
ECHO %last_modified_file%
:: Filename: test_if.cmd
:: How to execute: test_if.cmd hello
IF "%1"=="hello" (
SET the_greeting_message=Greetings, traveller.
)
IF "%the_greeting_message%"=="Greetings, traveller." (
ECHO %the_greeting_message%
)
问题在于,在if
命令行上的尾随(
字符前缺少空格:
IF "%last_modified_file%"=="init.marker"(
在尾随(
字符之前插入空格,它应该能够正确解析。在之前插入空格(if
命令行上的字符。@Bill_Stewart哇,这对我来说既令人失望又有点尴尬^^。您修复了它,非常感谢!您能发布一个答案让我接受吗?删除了不适用的bash
标记。