说明DOS的批处理如何来换行变量黑客作品 [英] Explain how dos-batch newline variable hack works

查看:684
本文介绍了说明DOS的批处理如何来换行变量黑客作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否有人请解释这是如何工作?

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.

从<一的 href=\"http://stackoverflow.com/questions/132799/how-can-you-echo-a-newline-in-batch-files/269819#269819\">How您可以在批处理文件呼应换行符?

推荐答案

诀窍使用符的行为。结果
此外,在<一个解释href=\"http://stackoverflow.com/questions/69068/long-commands-split-over-multiple-lines-in-vista-dos-batch-bat-file/4455750#4455750\">Long命令拆分为多行Vista中/ DOS批处理文件(.bat)

The trick use the behaviour of the caret.
Also explained at Long commands split over multiple lines in Vista/DOS 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.

在该行最后干脆逃跑下一个字符,在这种情况下,&LT;换行&GT; ,但有一个隐藏的功能,因此,如果转义字符是&LT; LF&GT; 被忽略,下一个字符被读取并逃过一劫,但这种系统字符会永远逃脱,即使它也是一个&LT ; LF&GT;

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变量包含一个&LT; LF&GT; 字符
但是,如果你尝试使用使用回声线路1%NLM%的2号线失败,因为解析器停在一个单一的解析&LT; LF&GT; 。结果
但是这个作品

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 = ^&LT; LF&GT;&LT; LF&GT;
如果这是扩大只创建一个逃出&LT; LF&GT; 作为第一个&LT; LF&GT;

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 creates only one escaped <LF> as the first <LF> after the caret will be ignored.

顺便说一句。在我看来,它是非常容易使用换行符延迟扩展,没有必要逃避什么。

Btw. In my opinion, it is much easier to use linefeeds with delayed expansion, as there is no need to escape anything.

setlocal EnableDelayedExpansion
set NLM=^


echo Line1!NLM!Line2

编辑:追加一些使用提示有用的&LT; LF&GT;

Append some hints for useful using the <LF>

1),使用它作为换行符在回声

1) Use it as newline in an echo

setlocal EnableDelayedExpansion
set LF=^


echo Line1!LF!Line2

2)使用它在一个括号块分割命令

2) Use it to split commands in a parenthesis block

setlocal EnableDelayedExpansion
set LF=^


(
    echo Line1%LF%set x=4%LF%echo !x!%LF%
)

在FOR / F环

3)创建(几乎)空EOL-chararcter,
&LT; LF&GT; 是分隔符&LT的EOL行; LF&GT; 比空单相同。

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=^


rem ** Two empty lines required
set "var=Content1;Content2"
FOR /F "delims=" %%a in ("%var:;=!LF!%") do (
  echo %%a
)

这篇关于说明DOS的批处理如何来换行变量黑客作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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