简体   繁体   English

批处理脚本中的符号 ^ 是什么意思?

[英]What does symbol ^ mean in Batch script?

In this command:在这个命令中:

FOR /F %%A IN ('TYPE "%InFile%"^|find /v /c ""')DO SET "Till=%%A"

what does the ^ mean? ^是什么意思?

The ^ symbol (also called caret or circumflex) is an escape character in Batch script. ^ 符号(也称为插入符或抑扬符)是批处理脚本中的转义字符。 When it is used, the next character is interpreted as an ordinary character.使用时,下一个字符被解释为普通字符。

In your script, the output of the TYPE command to be written as the input to the FIND command.在您的脚本中,TYPE 命令的输出将作为 FIND 命令的输入写入。
If you don't use the escape character ^ before the |如果您不在|之前使用转义字符 ^ , then the regular meaning of | ,然后是|的常规含义is a pipe character .是一个管道字符

The Documentation says: 文档说:

To display a pipe (|) or redirection character (< or >) when you are using echo, use a caret character immediately before the pipe or redirection character (for example, ^>, ^<, or ^| ).要在使用 echo 时显示管道 (|) 或重定向字符(< 或 >),请在管道或重定向字符之前使用插入符(例如, ^>、 ^< 或 ^| )。 If you need to use the caret character (^), type two (^^).如果需要使用插入符 (^),请键入两个 (^^)。

The caret '^' character serves two purposes in Windows batch files:插入符号“^”字符在 Windows 批处理文件中有两个用途:

1. line continuations: 1. 线路延续:

~~~ ~~~

@echo off

dir ^
/ad ^
c:\temp

~~~~ ~~~~

results in dir /ad c:\\temp, which lists only the directories in C:\\temp.结果是 dir /ad c:\\temp,它只列出 C:\\temp 中的目录。

2. Escaping reserved shell characters & | 2. 转义保留的 shell 字符 & | ( < > ^. ( < > ^.

Use a preceding caret to escape and print the character:使用前面的脱字符来转义并打印字符:

echo this pipe will print ^| but this one won't |
echo and this will print one caret ^^

Infinite Recursion describes the general behavior of ^ , but is a bit fuzzy on why it must be used within IN ('yourCommand') .无限递归描述了^的一般行为,但对于为什么必须在IN ('yourCommand')使用它有点模糊。

yourCommand is actually executed implicitly within its own CMD.EXE process using C:\\Windows\\system32\\cmd.exe /c yourCommand . yourCommand实际上是在它自己的 CMD.EXE 进程中使用C:\\Windows\\system32\\cmd.exe /c yourCommand隐式执行的。 Obviously the pipe must be included in the command in your case.显然,在您的情况下,管道必须包含在命令中。 But the entire line must be parsed by the batch parser before it can pass the IN() clause on to be executed.但是整行必须由批处理解析器解析,然后才能将 IN() 子句传递给执行。 Without the ^ , the |没有^| confuses the batch parser.混淆批处理解析器。 The ^ escapes (protects) the pipe during the initial batch parsing. ^在初始批处理解析期间转义(保护)管道。

In the given example the caret is used for escaping the special symbol.在给定的示例中,插入符号用于转义特殊符号。 Though it has other usages in the windows commands虽然它在 windows 命令中还有其他用途

1] In set it is a XOR operator: 1] 在集合中它是一个 XOR 运算符:

set /a "_num=5^3"    &::0101 XOR 0011 = 0110 (decimal 6)

2] In findstr is used for regular expressions of finding string at the beginning of a line: 2] 在 findstr 中用于在行首查找字符串的正则表达式:

Echo 12G6 |FindStr /R "[^0-9]"  &::this will check for non-numeric characters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM