REM语句中的%〜 [英] %~ in REM statement

查看:94
本文介绍了REM语句中的%〜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到Windows批处理文件中,即使您删除%〜也会收到错误消息

I've noticed in a windows batch file, even if you REM out a %~ you get the error

For valid formats type CALL /? or FOR /?
The syntax of the command is incorrect.

为什么注释行不被忽略?

Why isn't the commented line ignored?

@ ECHO OFF
REM Blah blah blah
REM %~ this will cause an error
REM %%~ Double the fun, no error

推荐答案

REM 是类似于 ECHO 的命令.在命令提示符窗口rem /?中运行以获取有关此命令的帮助.

REM is a command like ECHO. Run in a command prompt window rem /? to get help on this command.

Windows命令解释器首先像批处理文件中的所有其他行一样解析此命令行,除了那些以冒号开头的行(它们被解释为标签行而不是命令行)之外.

The Windows command interpreter first parses this command line like all other lines in a batch file with the exception of those lines starting with a colon which are interpreted as label line and not as command line.

因此,单个%会被解释为完全执行 REM 之前环境变量或批处理文件参数引用的开始.因为该命令行上没有更多的百分号,所以删除了单个百分号.

So a single % is interpreted as begin of an environment variable or batch file argument reference before REM is executed at all. The single percent sign is removed because there is no more percent sign on this command line.

%~在命令行解析中被解释为无效的批处理文件参数引用.对于Windows命令解释器而言,命令 REM 不评估该行的其余部分并不重要.首先,命令行是独立于命令进行解析的,其次命令是由cmd.exe执行的,无论该命令是什么.

%~ is interpreted on command line parsing as invalid batch file argument reference. That the command REM does not evaluate the rest of the line does not matter for Windows command interpreter. First the command line is parsed independent on the command and next the command is executed by cmd.exe whatever the command is.

删除第一行@ ECHO OFF,可以看到Windows命令行解释器在解析每个命令行后如何执行它们.以冒号开头的行不会输出为解释为标签线,而不会解释为命令行.

Remove first line @ ECHO OFF and it can be seen how each command line is executed after being parsed by Windows command interpreter. Lines starting with a colon are not output as being interpreted as label line instead of a command line.

有关批处理脚本解析的详细信息,请参见 Windows命令解释器(CMD.EXE)如何解析脚本?

For details about batch script parsing see How does the Windows Command Interpreter (CMD.EXE) parse scripts?

示例:

REM User name: %USERNAME%
REM User profile directory: %USERPROFILE%
REM Application data directory: %APPDATA%
REM Batch arguments: %0 %*
PAUSE

在这些 REM 命令行中的环境变量引用和批处理文件参数引用在运行此批处理文件时被环境变量和批处理文件参数的值替换,而顶部没有@ECHO OFF

The environment variable references and the batch file arguments reference in those REM command lines are replaced by the values of the environment variables and the batch file arguments on running this batch file without @ECHO OFF at top.

这篇关于REM语句中的%〜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆