Windows cmd echo/pipe最后增加了额外的空间-如何修剪它? [英] Windows cmd echo / pipe is adding extra space at the end - how to trim it?

查看:151
本文介绍了Windows cmd echo/pipe最后增加了额外的空间-如何修剪它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个脚本,该脚本执行一个命令行应用程序,该应用程序在运行时需要用户输入(没有提供不幸的命令行参数).

I'm working on a script that executes a command line application which requires user input at runtime (sadly command line arguments are not provided).

所以我的第一次尝试是这样的:

So my first attempt looked like this:

@echo off
(echo N
echo %~dp0%SomeOther\Directory\
echo Y)|call "%~dp0%SomeDirectory\SadSoftware.exe"

乍看之下,它看起来运作良好,但事实证明并非如此.经过调查,我发现要传递给软件的目录最后包含额外的空间,这导致了一些问题.

At first glance it looked like it worked pretty well, but as it turned out it didn't. After investigation I found out that the directory I was passing to the software contained extra space at the end, which resulted in some problems.

我环顾了一会儿,发现以下问题: echo与管道一起使用时会增加空间.

I looked around for a while and found out following question: echo is adding space when used with a pipe .

这说明了正在发生的事情,但并没有真正帮助我解决问题(我对批处理编程不太熟悉).

This explained what is going on, but didn't really help me solve my problem (I'm not too familiar with batch programming).

此刻,我通过一个丑陋的解决方法解决了"我的问题:

At the moment I "kind of solved" my problem with an ugly workaround:

echo N> omg.txt
echo %~dp0%SomeOther\Directory\>> omg.txt
echo Y>> omg.txt

"%~dp0%SomeDirectory\SadSoftware.exe"<omg.txt

del omg.txt

此解决方案有效,但我对此并不满意.有没有更漂亮的方法?甚至更丑陋的方式,但没有临时文件?

This solution works, but I'm less than happy with it. Is there some prettier way? Or even uglier way, but without temporary file?

推荐答案

解决方案1:换行符

您可以使用换行符来避免空格.
rem.是必需的,以避免cmd.exe注入的&问题

You can use a linefeed to avoid the spaces.
The rem. is required to avoid problems with the injected & by cmd.exe

SET LF=^


REM ** The two empts lines are required for the new line **

(
echo line1%%LF%%rem.
echo line2%%LF%%rem.
echo line3%%LF%%rem.
) | sort

通过管道传递块时,cmd.exe将通过在每条命令之间用单行与&"号组合起来来重建该块.

When a block is piped the cmd.exe will rebuild the block by building a single line combined with ampersands between each command.

(
echo line1
echo line2
) | sort

将被转换为

C:\Windows\system32\cmd.exe  /S /D /c" (<space>echo line1<space>&<space>echo line2<space>)"

所以换行技巧会导致类似

So the linefeed trick results in a line like

C:\Windows\system32\cmd.exe  /S /D /c" ( echo line1%LF%rem. & echo line2%LF%rem. )"

然后扩展为

( echo line1
rem. & echo line2
rem. )

解决方案2:逃脱的&符
您也可以直接使用&"号,但是需要将其转义,否则无论如何都会注入空格.
最后一个rem.是为了避免在最后一个回声之后出现空格

Solution2: Escaped ampersands
You can also use directly ampersands, but they need to be escaped, else the spaces are injected anyway.
The last rem. is to avoid a space after the last echo

( 
    echo Line1^&echo Line2^&echo Line3^&rem. 
) | more

解决方案3:创建单个命令
@MCND提到了直接使用表格的技巧:

Solution3: Create a single command
@MCND Mentioned the trick to use directly the form:

cmd /q /c"(echo line1&echo line2&echo line3)" | sort

这有效,因为解析器仅看到cmd命令,其余部分作为该单个命令的参数进行处理.
因此,空格没有任何问题. 或换行符看起来像

This works, as the parser only sees the cmd command, the rest is handled as parameters for this single command.
Therefore there aren't any problems with spaces. Or with the linefeeds it looks like

cmd /q /c"(echo line1%%LF%%echo line2%%LF%%echo line3)"| (sort > out.txt)

然后您也可以将其修改为

And then you can modify it also to

cmd /q /c^"(echo line1%%LF%%^
echo line2%%LF%%^
echo line3)^"| (sort > out.txt)

这篇关于Windows cmd echo/pipe最后增加了额外的空间-如何修剪它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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