带管使用时,回波增加空间 [英] echo is adding space when used with a pipe

查看:79
本文介绍了带管使用时,回波增加空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答<一个href=\"http://stackoverflow.com/questions/10943099/how-to-sort-the-arguments-dropped-to-a-batch-file/10943824#10943824\">this问题我发现了一些奇怪的行为,我没有解释

While answering this question I found some strange behavior for which I have no explanation

for /f "delims=" %a in ('(for /l %z in (1,1,10^) do @echo %z^)') do @echo %a0

您会看到数字10..100,现在只需添加管道,如到排序更多,无论

You'll see numbers 10..100, now just add the pipe, e.g. to sort or more, whatever:

for /f "delims=" %a in ('(for /l %z in (1,1,10^) do @echo %z^)^|sort') do @echo %a0

有会之间添加空格%A 0
貌似回声ING通过管东西添加一个尾随的空间,可以很容易地看出:

There'll be spaces added between %a and 0! Looks like echo-ing something through a pipe adds a trailing space, it can be easily seen:

>_tempfile echo no space here
>_tempfile echo and here's a space|more

甚至

>_tempfile <nul set /p =also a space|sort

(可能使用回声打印提示)

(probably uses echo to print the prompt)

在没有输出重定向(无论是到文件或命令)这不会发生。
它是一个错误还是我失去了一些东西?我该如何摆脱的空间? (除了剥离的最后一个字符用 VAR的肮脏的黑客:〜0,-1

This doesn't happen when there's no output redirection (whether into a file or for command). Is it a bug or am I missing something? How do I get rid of the space? (besides the dirty hack of stripping the last character with var:~0,-1)

推荐答案

出色和有趣的问题(+1)

Excellent and interesting question (+1)

的空间由在CMD解析器的管机制引入,而不是由SORT

The space is introduced by the pipe mechanism of the CMD parser, not by SORT.

当您执行使用FOR / F命令,该命令在其自身的CMD shell中执行。此外,管的每一侧在其自己的CMD壳被执行。请参见为什么推迟扩张失败时,code的管道块内?,获取更多信息。

When you execute a command using FOR /F, the command is executed in its own CMD shell. Also, each side of a pipe is executed in its own CMD shell. See Why does delayed expansion fail when inside a piped block of code? for more info.

所以你的命令实际上实例3 CMD壳,一个用于FOR / F命令,从而为管道的每个侧面2。

So your command actually instantiates 3 CMD shells, one for the FOR /F command, which in turn creates 2 for each side of the pipe.

您可以看到的命令是如何被解析并送入使用%CMDCMDLINE%的动态变量的CMD外壳。因为我们在命令行中执行命令,我们需要逃避的变量名至少有一个字符两次,它不会得到扩展,直到它达到最内层CMD壳。

You can see how the commands get parsed and fed into the CMD shell using the %CMDCMDLINE% dynamic variable. Because we are executing the command from the command line, we need to escape at least one character in the variable name twice so that it doesn't get expanded until it reaches the inner most CMD shell.

下面是对结果的命令(领导&GT; 是我的命令提示符):

Here is the command with the results (the leading > is my command prompt):

>for /f "delims=" %a in ('(echo %^^^cmdcmdline%^&for /l %z in (1,1,10^) do @echo %z^)^|sort') do @echo %a0
1 0
10 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
C:\Windows\system32\cmd.exe  /S /D /c" ( echo %cmdcmdline% & FOR /L %z in (1 1 10) do @ echo %z )" 0

输出的最后一行是用于管的左侧的命令行。你可以看到解析器许多地方如何增加空间。

The last line of output is the command line used for the left side of the pipe. You can see how the parser added spaces in a number of places.

您可以通过使用一个简单的批处理脚本回显值,而不是echo命令绕过这个问题。

You can circumvent the problem by using a simple batch script to echo the value instead of the ECHO command.

echoArgs.bat

@echo(%*

现在,当您运行此命令,你得到期望的结果。

Now when you run this command you get the desired result

>for /f "delims=" %a in ('(for /l %z in (1,1,10^) do @echoArgs %z^)^|sort') do @echo %a0
10
100
20
30
40
50
60
70
80
90

要绕过这个问题的另一种方法是适当地创建一个变量与echo命令和逃避变量的扩展。

Another method to circumvent the problem is to create a variable with your ECHO command and escape the expansion of the variable appropriately.

>set cmd=@(echo %z)

>for /f "delims=" %a in ('(for /l %z in (1,1,10^) do %^^^cmd%^)^|sort') do @echo %a0
10
100
20
30
40
50
60
70
80
90


修改


EDIT

&GT; _tempfile回声和这里的空间|更多的例子也很有趣。有在输出文件的末尾的额外的空间。但乍得Nouis是正确的,没有什么被分类,因为左侧的重定向。任何命令可能在右侧被使用,其结果将是相同的。

The >_tempfile echo and here's a space|more example is also interesting. There is an extra space at the end of the output file. But Chad Nouis is correct that nothing is being sorted because of the redirection of the left side. Any command could be used on the right side and the result would be the same.

问题的来源仍然解析器,但方式解析器重组的条命令是有趣

The source of the problem is still the parser, but the way the parser restructures the commmand is interesting.

>>_tempfile echo %^cmdcmdline%|rem

>type _tempfile
C:\Windows\system32\cmd.exe  /S /D /c" echo %cmdcmdline% 1>_tempfile"

注意如何重定向从开始移动到命令的末尾,而 1 的文件句柄明确添加。你当然可以看到额外的空间从何而来。

Notice how the redirection is moved from the beginning to the end of the command, and the file handle of 1 is explicitly added. You can certainly see where the extra space comes from.

这篇关于带管使用时,回波增加空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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