|
logictianjin
初级用户
 
积分 58
发帖 25
注册 2006-11-29
状态 离线
|
『楼 主』:
请教for语句中出现的^符号的含义![已结]
使用 LLM 解释/回答一下
for /f "tokens=3" %%b in ('dir /-c %%a:\^|findstr "可用字节"')
1.请拆解说明一下这个FOR命令,并注明一下 ^起了什么作用
2.如果 ^是转义的作用,请说明一下为什么要用转义!
Last edited by logictianjin on 2007-4-11 at 01:08 PM ]
### Part 1: Disassemble and explain the FOR command
The `for /f "tokens=3" %%b in ('dir /-c %%a:\^|findstr "Available bytes"')` command is a `for` loop command in batch scripting.
- `for /f`: This is the specific form of the `for` loop used for processing the output of a command. The `/f` option indicates that it will parse the output of a command.
- `"tokens=3"`: This option specifies that we want to extract the third token from each line of the input. A "token" is a string of characters separated by delimiters (by default, spaces and tabs are delimiters).
- `%%b`: This is a variable that will hold the value of the third token extracted in each iteration.
- `('dir /-c %%a:\^|findstr "Available bytes"')`: This is the command whose output is being processed.
- `dir /-c %%a:\`: The `dir` command is used to list directory contents. The `/ -c` option is used to display file sizes in bytes with thousand - separator. `%%a:\` is a placeholder for a drive letter (since `%%a` is a loop variable that would be set to a drive letter in an outer loop context).
- `^|`: The `^` is an escape character here. In a batch file, the `|` (pipe) character has special meaning, and using `^` before it escapes the pipe character so that it is passed as part of the command to be executed by the inner `cmd` instance.
- `findstr "Available bytes"`: The `findstr` command is used to search for a specific string ("Available bytes" in this case) in the output of the preceding command.
### Part 2: Explanation of why the escape character `^` is used
The `|` (pipe) character has a special meaning in the batch file parsing context. When the `for /f` command is processing the command string inside the parentheses, the batch parser would try to interpret the `|` as a command - line pipe operator before the inner `cmd` has a chance to execute it. By using `^` to escape the `|`, we are telling the batch parser to treat the `|` as a literal character that should be passed to the `cmd` instance to be used as a pipe operator between the `dir` command and the `findstr` command. So, the `^` is used to prevent the batch parser from misinterpreting the `|` as a special character before the inner command is executed.
|
|
2007-4-11 01:57 |
|
|
NaturalJ0
银牌会员
    
积分 1181
发帖 533
注册 2006-8-14
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
的确是转义的作用
不转的话 | 直接在这句中起它的特殊作用
转了|只是个一般字符
Indeed, it's the role of escaping. If not escaped, it directly plays its special role in this sentence. If escaped, it's just a general character.
|
|
2007-4-11 02:45 |
|
|
everest79
金牌会员
      一叶枝头,万树皆春
积分 2564
发帖 1127
注册 2006-12-25
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
在for内使用^是因为|管道符在cmd内有着分割命令的作用
for /f "tokens=3" %%b in ('dir /-c %%a:\|findstr "可用字节"')这一行不使用^那么cmd会识别为
for /f "tokens=3" %%b in ('dir /-c %%a:\
与
findstr "可用字节"')
The use of ^ inside the for is because the | pipe character has the function of separating commands in cmd. In the line "for /f "tokens=3" %%b in ('dir /-c %%a:\|findstr "可用字节"')", if ^ is not used, then cmd will recognize it as
for /f "tokens=3" %%b in ('dir /-c %%a:\
and
findstr "可用字节"')
|
|
2007-4-11 06:18 |
|
|
logictianjin
初级用户
 
积分 58
发帖 25
注册 2006-11-29
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
Originally posted by everest79 at 2007-4-10 05:18 PM:
在for内使用^是因为|管道符在cmd内有着分割命令的作用
for /f "tokens=3" %%b in ('dir /-c %%a:\|findstr "可用字节"')这一行不使用^那么cmd会识别 ...
在for内使用^是因为|管道符在cmd内有着分割命令的作用对于这句话不太理解.
下面是ntcmds帮助中对于 | 的解释:
管道操作符 (|) 可以提取一个命令的输出(默认情况下是 STDOUT),然后将其导入另一个命令的输入中(默认情况下是 STDIN)。
在此例中,()中的字符串已经用单引号括了起来,这是将其中的字符串看做是一个命令 可如果加了^符号,那不就成了让|不起作用,只作为一个单独的符号来显示吗? 越说越糊涂了,可否再详细的讲一讲!
Originally posted by everest79 at 2007-4-10 05:18 PM:
The use of ^ inside the for is because the | pipe character has the function of splitting commands in cmd
In the line for /f "tokens=3" %%b in ('dir /-c %%a:\|findstr "Available bytes"'), if ^ is not used, then cmd will recognize...
The use of ^ inside the for is because the | pipe character has the function of splitting commands in cmd I don't quite understand this sentence.
The following is the explanation of | in the ntcmds help:
The pipe operator (|) can extract the output of one command (by default STDOUT) and then import it into the input of another command (by default STDIN).
In this example, the string in () has been enclosed in single quotes, which treats the string inside as a command. But if the ^ symbol is added, doesn't that make | not work and just display it as a separate symbol? The more I talk, the more confused I get. Can you explain it in more detail!
|
|
2007-4-12 00:54 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
只需记着在for命令in后的括号里面,管道符前面都要加^前导符,这是规定。
Just remember that in the brackets after in in the for command, you need to add the ^ prefix before the pipe character. This is a regulation.
|
|
2007-4-12 01:02 |
|
|
logictianjin
初级用户
 
积分 58
发帖 25
注册 2006-11-29
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
Originally posted by lxmxn at 2007-4-11 12:02 PM:
只需记着在for命令in后的括号里面,管道符前面都要加^前导符,这是规定。
呵呵,大哥解释的确实通俗易懂,而且非常好记,可我希望从根本上理解这个问题,能否告诉下,如果想知道这个问题的究竟,需要参阅哪些资料?再次感谢!
Originally posted by lxmxn at 2007-4-11 12:02 PM:
Just remember that before the pipe symbol in the parentheses after in in the for command, you need to add the ^ prefix, which is a regulation.
Hehe, the elder brother's explanation is indeed easy to understand and very easy to remember, but I hope to understand this problem fundamentally. Can you tell me which materials I need to refer to if I want to know the details of this problem? Thank you again!
|
|
2007-4-12 01:16 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
你可以搜索一下 willsort 兄关于for命令的详细分析相关的贴子。
You can search for Brother willsort's detailed analysis post about the for command.
|
|
2007-4-12 01:18 |
|
|
everest79
金牌会员
      一叶枝头,万树皆春
积分 2564
发帖 1127
注册 2006-12-25
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Originally posted by logictianjin at 2007-4-11 11:54 AM:
在for内使用^是因为|管道符在cmd内有着分割命令的作用对于这句话不太理解.
下面是ntcmds帮助中对于 | 的解释:
管道操作符 (|) 可以提取一个命令的输出(默认情况下是 STDOUT),然后将其导入另一个命令的输入中(默认情况下是 STDIN)。
在此例中,()中的字符串已经用单引号括了起来,这是将其中的字符串看做是一个命令 可如果加了^符号,那不就成了让|不起作用,只作为一个单独的符号来显示吗? 越说越糊涂了,可否再详细的讲一讲!
在教材中|&也是一种命令,可以组合其它被充许的命令,你反过来理解不就是分割吗
CMD解释命令是逐行的,当读入一行组合命令如echo a&echo b时,他在内部处理时自然会将这行拆分为两个命令行(echo a与echo b,这里是抽象举例),但也在这两个命令行中形成了一个继承或条件关系,先在这里称之为组合初始化
FOR %variable IN (set) DO command
CMD在解释FOR命令时,首先的工作是将一个完整的FOR命令读入,但在读取(set)中若发现|&符号,便会进行上述的组合初始化工作,很显然,CMD会在这里报错,命令是不能正确执行的,但为什么加上^符号便可以正常执行,而不是让CMD将|&当做单纯的字符来处理?
其实CMD的确将加了^的|&符号当做字符来处理了,而只所以能正常执行,是因为这一步处理是发生在CMD读入FOR整个命令行的过程中,而不是FOR接管后将(set)提交给CMD的过程中,下边来看看过程
CMD读入前:for /f %i in (echo a^&echo b) do echo %i
CMD读入后:for /f %i in (echo a&echo b) do echo %i
FOR提交时:CMD<(echo a&echo b)
Originally posted by logictianjin at 2007-4-11 11:54 AM:
The reason for using ^ inside the for is because the | pipe character has the role of splitting commands in cmd. I don't quite understand this sentence.
The following is the explanation of | in the ntcmds help:
The pipe operator (|) can extract the output of one command (by default, STDOUT) and then import it into the input of another command (by default, STDIN).
In this example, the string in () has been enclosed in single quotes, which treats the string inside as a command. But if the ^ symbol is added, doesn't that make the | not work and just appear as a separate symbol? The more I talk, the more confused I get. Can you explain it in more detail!
In the textbook, &| is also a kind of command, which can combine other allowed commands. If you understand it in reverse, isn't it splitting?
CMD interprets commands line by line. When reading a combined command such as echo a&echo b, it will naturally split this line into two command lines (echo a and echo b, this is an abstract example here) during internal processing, but it also forms an inheritance or conditional relationship in these two command lines. Let's call it combined initialization here.
FOR %variable IN (set) DO command
When CMD interprets the FOR command, the first task is to read a complete FOR command. But when reading (set), if |& symbols are found, the above combined initialization work will be carried out. Obviously, CMD will report an error here, and the command cannot be executed correctly. But why can it be executed normally when the ^ symbol is added, instead of letting CMD treat |& as a simple character?
In fact, CMD does treat the |& symbol with ^ added as a character. The reason why it can be executed normally is that this processing step occurs during the process of CMD reading the entire FOR command line, not during the process of FOR taking over and submitting (set) to CMD. Let's take a look at the process below.
Before CMD reads: for /f %i in (echo a^&echo b) do echo %i
After CMD reads: for /f %i in (echo a&echo b) do echo %i
When FOR submits: CMD<(echo a&echo b)
|
|
2007-4-12 08:09 |
|
|
logictianjin
初级用户
 
积分 58
发帖 25
注册 2006-11-29
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Originally posted by everest79 at 2007-4-11 07:09 PM:
在教材中|&也是一种命令,可以组合其它被充许的命令,你反过来理解不就是分割吗
CMD解释命令是逐行的,当读入一行组合命令如echo a&echo b ...
非常感谢 完全明白了,如果还有看了不明白的朋友请追帖,我来给解释就可以了,哈哈,把问题参透的感觉真是好!
Originally posted by everest79 at 2007-4-11 07:09 PM:
In the textbook, |& is also a command that can combine other allowed commands. If you understand it in reverse, isn't it splitting? The CMD interprets commands line by line. When reading a combined command line such as echo a&echo b...
Thank you very much. I fully understand. If there are friends who still don't understand after reading, please reply, and I will explain it. Haha, the feeling of thoroughly understanding the problem is really good!
|
|
2007-4-12 09:45 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
|
2007-4-12 10:28 |
|
|
everest79
金牌会员
      一叶枝头,万树皆春
积分 2564
发帖 1127
注册 2006-12-25
状态 离线
|
|
2007-4-12 10:47 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
To everest79:
嘿嘿,多谢兄的鼓励哈。
To everest79:
Hehe, thanks for your encouragement, brother.
|
|
2007-4-12 14:07 |
|
|
huahua0919
银牌会员
    
积分 1608
发帖 780
注册 2007-10-7
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
个人的实践不能成为理论
那别人的理论来实践才踏实
Personal practice cannot become a theory. Only by applying someone else's theory in practice can one feel secure.
|
|
2007-11-25 20:58 |
|
|
lving
新手上路

积分 11
发帖 6
注册 2009-10-20
状态 离线
|
|
2009-10-24 14:05 |
|
|
lovelymorning
初级用户
 
积分 131
发帖 72
注册 2008-2-24
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
楼上说得没错。。。ms说得更是没错。。。。官方语言嘛。。。
cmd,处理命令时,都是先读取整句命令,然后进行预处理
不管你这条命令多复杂,写了多少行。。。cmd,都视为一条命令
就说上面的 for,for的 集里面,可以有好多命令组合。。do也可以写好多命令
但对于cmd来说,这些,都是一条命令,for命令。。。
for /f "tokens=3" %%b in ('dir /-c %%a:\|findstr "可用字节"')
如果写成这样。。。cmd读取时,这是一条for命令,但是,for之后没有do,语法错误
也可以理解成 这条命令,被 | 分成了两条。。。
第一条是 for /f "tokens=3" %%b in ('dir /-c %%a:\
第二条是 findstr "可用字节"')
这两条,都是错误命令,先不说for错误,就光 括号 ,有前括没回括。。。而findstr缺少所查找文件
所以,从哪个方面来说,这命令,都是错误命令
而加上 ^,那么,| 对于for来说,就是普通字符,但是,对于for里面的集来说,就是一个特殊符号
为何呢?
cmd读取时,是一条for命令,完整读入
因为有 ^ ,所以 | 是普通字符,所以,命令是正确的。。
经过cmd对for的预处理,只有一个的 ^ 被cmd脱去。。。
而当cmd处理for的集时,因为 ^ 已经脱去了,所以变成了
dir /-c %%a:\|findstr "可用字节"
这个样子。。。| 没人转义它,那它就是实实在在的特殊字符了。。所以,命令正确
个人见解。。。实际上,要解释这个,就得从cmd的预处理机制和 | 命令作用以及cmd对 | 命令的处理机制来解释。。。
如果从这方面来解释。。估计,越看越晕的
反正,记住,for的 集中,如果要用到 | || & && > >> 等特殊字符时,都需要转义,,,这样就同错了。。
想知为何,建议去看一下cmd预处理机制和对特殊命令的处理机制
What the person upstairs said is correct... What MS said is even more correct... It's the official language...
When cmd processes commands, it first reads the entire command line and then preprocesses it.
No matter how complicated this command is and how many lines it is written in... cmd treats it as one command.
Just take the above for example. In the set of for, there can be many command combinations. The do can also write many commands. But for cmd, these are all one command, the for command...
for /f "tokens=3" %%b in ('dir /-c %%a:\|findstr "Available bytes"')
If written like this... when cmd reads it, this is a for command, but there is no do after for, which is a syntax error.
It can also be understood as this command is divided into two parts by |.
The first part is for /f "tokens=3" %%b in ('dir /-c %%a:\
The second part is findstr "Available bytes"')
Both of these are incorrect commands. Let's not talk about the for error first. Just the parentheses, there is an opening parenthesis but no closing parenthesis. And findstr is missing the file to search for. So in any respect, this command is an incorrect command.
And adding ^, then | is an ordinary character for for, but for the set inside for, it is a special symbol.
Why?
When cmd reads, it is a for command, read in completely.
Because there is ^, so | is an ordinary character, so the command is correct.
After cmd's preprocessing of for, only one ^ is removed by cmd...
And when cmd processes the set of for, because ^ has been removed, it becomes
dir /-c %%a:\|findstr "Available bytes"
In this form... | is not escaped, so it is a real special character. So the command is correct.
Personal opinion... Actually, to explain this, one has to explain from cmd's preprocessing mechanism, the role of | command, and cmd's processing mechanism for | command...
If explained from this aspect... I'm afraid it will get more and more confusing.
Anyway, remember that in the set of for, if you need to use special characters such as | || & && > >>, you need to escape them... That's the same mistake.
If you want to know why, it is recommended to look at cmd's preprocessing mechanism and the processing mechanism for special commands.
此帖被 +1 点积分 点击查看详情 评分人:【 rivch 】 | 分数: +1 | 时间:2010-4-25 01:48 |
|
|
|
2009-10-24 21:23 |
|
|