解释 Windows 批处理换行变量 hack 的工作原理 [英] Explain how Windows batch newline variable hack works

查看:27
本文介绍了解释 Windows 批处理换行变量 hack 的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下这是如何工作的吗?

Can someone please explain how this works?

@echo off
REM Creating a Newline variable (the two blank lines are required!)
set NLM=^


set NL=^^^%NLM%%NLM%^%NLM%%NLM%
REM Example Usage:
echo There should be a newline%NL%inserted here.

发射:

There should be a newline
inserted here.

来自 你怎么能在批处理文件中回显换行符?

推荐答案

这个技巧使用了插入符号的行为.
也在 长解释命令在 Windows Vista 批处理 (.bat) 文件中拆分为多行

The trick uses the behaviour of the caret.
Also explained at Long commands split over multiple lines in Windows Vista batch (.bat) file

插入符号是下一个字符的转义字符,或者在行尾用作多行字符,但这几乎相同.

The caret is an escape character for the next character, or at the line end it is used as multiline character, but this is nearly the same.

在行尾它只是转义下一个字符,在这种情况下是 <Linefeed>,但是有一个隐藏特性,所以如果转义字符是 <LF> 它被忽略并读取下一个字符并进行转义,但该字符将始终被转义,即使它也是一个 .

At the line end it simply escapes the next character, in this case the <Linefeed>, but there is a hidden feature, so if the escaped character is a <LF> it is ignored and the next character is read and escaped, but this charater will be always escaped, even if it is also a <LF>.

现在你可以理解了

set NLM=^


rem Two empty lines are required here

NLM 变量只包含一个 字符.但是,如果您尝试将其与 echo Line1%NLM%Line2 一起使用,则会失败,因为解析器会在单个 处停止解析.
但这有效

The NLM-Variable contains exactly one <LF> character. But if you try to use it with echo Line1%NLM%Line2 it fails, as the parser stops parsing at a single <LF>.
But this works

echo Line1^

Line2

所以你需要在行中添加一个转义的换行符,那就是 NL 变量.NL 变量仅包含三个字符.
NL=^如果它被扩展,它只会创建一个转义的 作为插入符号后的第一个 将被忽略.

So you need to add an escaped linefeed into the line and that is the NL-Variable. The NL-Variable consists of only three characters.
NL=^<LF><LF> And if this is expanded, it creates only one escaped <LF> as the first <LF> after the caret will be ignored.

顺便说一句.在我看来,使用带有延迟扩展的换行符要容易得多,因为不需要转义任何东西.
在这个例子中,我使用 %=EMPTY=% 而不是空行(用于自我评论),但由于变量 =EMPTY= 不能存在,它将被扩展到一个空行.

Btw. In my opinion, it is much easier to use linefeeds with delayed expansion, as there is no need to escape anything.
In this example I use %=EMPTY=% instead of an empty line (for self commenting), but as the variable =EMPTY= can't exists it will be expanded to an empty line.

setlocal EnableDelayedExpansion
(set NLM=^
%=EMPTY=%
)
echo Line1!NLM!Line2

附加一些使用

1) 将其用作 echo

setlocal EnableDelayedExpansion
(set LF=^
%=EMPTY=%
)
echo Line1!LF!Line2

2) 用它来分割括号中的命令

2) Use it to split commands in a parenthesis block

setlocal EnableDelayedExpansion
(set LF=^
%=EMPTY=%
)
(
    echo Line1%LF%set x=4%LF%echo !x!%LF%
)

3) 在 FOR/F 循环中创建一个(几乎)空的 EOL 字符,由于 是行分隔符, 的 EOL 与空的相同.

3) Create a (nearly) empty EOL-chararcter in a FOR /F loop, as <LF> is the line delimiter an EOL of <LF> is the same than an empty one.

FOR /F ^"eol^=^

delims^=^" %%a in (myFile.php) do echo %%a

4) 使用 LF 在 FOR/F 循环中拆分文本

4) Use LF for splitting text in a FOR /F loop

setlocal EnableDelayedExpansion
(set LF=^
%=EMPTY=%
)
set "var=Content1;Content2"
FOR /F "delims=" %%a in ("%var:;=!LF!%") do (
  echo %%a
)

这篇关于解释 Windows 批处理换行变量 hack 的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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