如何在命令行中可靠地使用`rem`而不忽略相邻命令? [英] How to reliably use `rem` within a command line without ignoring adjacent commands?
问题描述
我正在尝试使用 rem
命令在包含多个命令的命令行中放置备注。这里有一些例子来说明我的意思:
I am trying to use the rem
command to place a remark in a command line that contains several commands. Here are some examples to illustrate what I mean:
echo Hello & rem.Comment & echo world!
(echo Hello & rem.Comment) & echo world!
这很好用,两个 echo
命令每行都按我的预期执行。 。
似乎修改了 rem
命令的行为,因此它不会将其余行视为注释:
This works perfectly fine, both echo
commands in each line are executed as I expect. The .
seems to modify the behaviour of the rem
command so that it does not treat the remaining line as comment:
Hello
world!
如果我放置了 SPACE (或任何其他定界符 TAB ,,
,;
, =
)而不是。
,其余行以及第二个 echo
将被忽略。 (在第二个示例中,出现 More?
提示,因为)
是备注的一部分,而 cmd
预计收盘)
,因为(
):
If I placed a SPACE (or any other delimiter TAB, ,
, ;
, =
) instead of the .
, the remaining line and therefore the second echo
would be ignored (for the second example a More?
prompt appears, because the )
is part of the remark and cmd
expects a closing )
because of the (
):
Hello
我发现除了。
,以下字符也起作用::
, /
, \
, [
,]
和 +
。
其他有效的方法是转义分隔符: ^
空格, ^
TAB , ^ ,
, ^;
和 ^ =
。
I found out that beside .
, the following characters work as well: :
, /
, \
, [
, ]
and +
.
What else works is escaped delimiters: ^
SPACE, ^
TAB, ^,
, ^;
and ^=
.
但是,是否有安全可靠的方法来做到这一点?
我会很高兴看到一个适用于命令提示符和批处理文件的解决方案。
I would be very glad about a solution that works for both command prompt and batch-files.
根据此外部引用,即熟悉的语法 echo。
返回空行在某些情况下会失败,因此建议使用 echo(
,因为这是唯一可靠的方法。
According to this external reference, the familiar syntax echo.
for returning a blank line fails under certain circumstances, hence using echo(
is recommended as this is the only reliable method.
但是,对于 rem
,(
不起作用, rem(
无法识别为命令。
However, for rem
, the (
does not work, everything after rem(
is not recognised as a command.
因为我知道Windows XP中 rem
命令的怪异错误(参考此外部链接: rem%〜
),我对适用于Windows Vista,Windows 7或更高版本的解决方案感兴趣。
Since I am aware of a weird bug of the rem
command in Windows XP (reference this external link: rem %~
), I am interested in a solution that applies to Windows Vista, Windows 7 or higher.
推荐答案
怪异 REM%〜
bug不是仅限XP。它存在于使用CMD.EXE的Windows的所有现代版本中。阅读完您的问题后,我给SS64的西蒙写了笔记,以澄清问题。如果变量 var 存在,并且您有 rem%var:=
,那么REM也会失败。
The "weird" REM %~
"bug" is not limited to XP. It is present in all modern versions of Windows that use CMD.EXE. After reading your question, I wrote Simon of SS64 a note to give clarification on the issue. REM can also fail if variable var exists, and you have rem %var:=
.
因此,从技术上讲,没有保证安全的方法来盲目使用REM。
So technically, there is no guaranteed safe way to blindly use REM.
但是,如果您愿意接受致命的%扩展风险,则列出的大多数产品骇客是可以安全使用的,但仅(如果该行至少包含一个通过&
或&& ;
。
But, if you are willing to accept the fatal % expansion risk, most of your listed hacks are safe to use, but only if the line includes at least one additional command via &
or &&
.
REM。
绝对不会在任何情况下安全使用一个名为 REM
(无扩展名)的文件。
REM.
is never safe to use in any situation if there exists a file named REM
(without extension).
文件夹分隔符 \ 如果当前文件夹中包含名为
test.bat
的文件,并且您使用<和
。 /
总是失败。 code> REM\..\test.bat
The folder dividers \
and /
always fail if the current folder contains a file named test.bat
and you use REM\..\test.bat
.
以类似的方式, REM :\..\test.bat
总是会失败。
In a similar fashion, REM:\..\test.bat
always fails.
在类似情况下,其他所有黑客都可能独立失败。例如, REM ^ [tab] \..\test.bat
独立失败,但如果与另一个命令连接则可以工作。这是我发现的唯一情况,其中 +
, [
,]
或 ^ [tab]
可能会失败。
Every one of the other hacks can fail stand-alone in a similar situation. For example, REM^[tab]\..\test.bat
fails stand-alone, but works if concatenated with another command. This is the only type of situation I've found where +
, [
, ]
, or ^[tab]
can fail.
在某些情况下,某些情况
There are additional cases where some of the other hacks can fail.
集合C中的任何字符( ^ [space]
, ^,
, ^;
, ^ =
)在文件名中有效如果存在 remC.bat
,则可以独立失败。例如,以下独立失败:
Any character in the set C (^[space]
, ^,
, ^;
, ^=
) that are valid in file names can fail stand-alone if remC.bat
exists. For example, the following fails stand-alone:
rem^ Fails if "rem .bat" exists
但是,当与另一个命令连接时,它们都是安全的:
Yet they are all safe when concatenated with another command:
echo OK&rem^ This is safe
rem^ This is safe &echo OK
临时更新
Temporary Update
以上某些错误。正在 http:// www进行调查.dostips.com / forum / viewtopic.php?f = 3& t = 6895& p = 44813#p44813 。
我相信以下是保证在所有情况下都能正常工作的最简单形式(不考虑无效的%扩展)
I believe the following are the simplest forms that are guaranteed to work in all cases (disregarding invalid % expansion)
REM: At least one space (or other token delimiter) must be after :
REM\ At least one space (or other token delimiter) must be after \
REM/ At least one space (or other token delimiter) must be after /
REM^[tab] At lease one space (or other token delimiter) must be after [tab]
但是在尘埃落定之前,我不会更正先前的信息
But I won't correct the earlier info until the dust has settled
结束临时更新
End Temporary Update
我最喜欢的使用内联注释的方法是使用不可能的变量。只有动态伪变量的名称中可以包含 =
,并且任何变量名称都不能包含两个 =
。所以我喜欢使用%=注释在这里=%
。这种形式的优点在于,只要注释中不包含%
或:$ c,它几乎可以在任何地方不受惩罚地使用。 $ c>。甚至可以在括号内的代码块中安全地使用它。
My favorite way to use inline comments is to use impossible variables. Only dynamic pseudo variables can contain =
in a name, and no variable name can ever contain two =
. So I like to use %= Remark goes here =%
. The beauty of this form is it can be used pretty much anywhere with impunity, as long as the comment does not contain %
or :
. It can even be used safely within parenthesized blocks of code.
for %%F in (*) do (
%= Comment within code block =%
%= 2nd comment within code block =%
FINDSTR /B %=Must match beginning of line=% "string" %= Search string =% "%%F" %= File to search =%
)
这篇关于如何在命令行中可靠地使用`rem`而不忽略相邻命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!