Windows等效的“echo -n”不再工作在Win7 [英] Windows equivalent of "echo -n" no longer works in Win7

查看:90
本文介绍了Windows等效的“echo -n”不再工作在Win7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows cmd.exe (至少达到XP)中有一个很好的技巧来模拟UNIX回声的行为,没有换行符, echo -n 。例如,命令:

 < nul:set / p junk = xyzzy 



会导致正好输出六个字符,前导空格和字符串xyzzy,而没有别的。


如果您对为什么感兴趣,它实际上是一个输入命令,输出xyzzy作为提示符,然后在将该输入分配给 junk 环境变量之前等待用户输入。在这种特殊情况下,它不等待用户输入,因为它从 nul 设备获取输入。


cmd 脚本中,当(例如)在一个循环中处理文件(每个文件一个迭代)每行多于一个。通过使用这个技巧,你可以简单地输出每个文件名后跟一个空格,没有换行符,然后,循环后,输出一个换行结束:

 处理文件:
file1.txt file42.txt p0rn.zip

现在我发现,在Windows 7下,空格不再输出,所以我得到的是:

 处理文件:
file1.txtfile42.txtp0rn.zip

有一种方法我可以得到 set / p 以开始再次尊重我的空间,或者在Win7中有另一种方式实现相同的效果吗?



尝试引用,使用(它在 echo 中工作),甚至转义字符串 ^ ,但它们都不起作用:

  C:\Pax> < nul:set / p junk = xyzzy 
xyzzy

C:\Pax> < nul:set / p junk =xyzzy
xyzzy

C:\Pax> < nul:set / p junk ='xyzzy'
'xyzzy'

C:\Pax> < nul:set / p junk =。 xyzzy
。 xyzzy

C:\Pax> < nul:set / p junk = ^ xyzzy
xyzzy

  C:\Pax> some_magical_command_with_an_argument xyzzy 
xyzzy

这将给我在开始时的空格

/ code>或其他 cmd - 内部的东西,但你可以使用 cscript Windows安装)做类似的事情。您甚至可以通过使用创建临时 cmd 文件本身(无需单独的文件)来控制它。



将此代码放在 cmd 文件中:

  rem创建VBS文件以输出空格和单词。 

echo.for i = 1到WScript.Arguments.Item(0)> spacetext.vbs
echo。 WScript.StdOut.Write ^^>>  spacetext.vbs
echo.next>> spacetext.vbs
echo.WScript.StdOut.Write WScript.Arguments.Item(1)> ;> spacetext.vbs

rem对于你想要输出的每一个字执行一次(例如,在一个循环中)。

cscript / nologo spacetext.vbs 0你好,
cscript / nologo spacetext.vbs 1 my
cscript / nologo spacetext.vbs 1 name
cscript / nologo spacetext。 vbs 1是
cscript / nologo spacetext.vbs 4 Pax。

rem在末尾添加换行符和kill temp文件。

echo。
del spacetext.vbs

这个输出是你期望的:

 您好,我的名字是Pax。 


I had a nifty trick in Windows cmd.exe (at least up to XP) to emulate the behaviour of the UNIX echo without a newline, echo -n. For example, the command:

<nul: set /p junk= xyzzy

would result in exactly six characters being output, the leading space and the string "xyzzy", and nothing else.

If you're interested in why this works, it's actually an input command which outputs " xyzzy" as the prompt then waits for user input before assigning that input to the junk environment variable. In this particular case, it doesn't wait for user input since it grabs the input from the nul device.

It was rather useful in cmd scripts when (for example) processing files in a loop (one iteration per file) where you want to list more than one per line. By using this trick, you could simply output each file name followed by a space and no newline then, after the loop, output a newline to finish up:

Processing files:
    file1.txt file42.txt p0rn.zip

Now I discover that, under Windows 7, the spaces are no longer output so what I get is:

Processing files:
file1.txtfile42.txtp0rn.zip

Is there a way I can get set /p to start honouring my spaces again, or is there another way in Win7 to achieve the same effect?

I've tried quoting, using . (which works in echo) and even escaping the string with ^, but none of them seem to work:

C:\Pax> <nul: set /p junk= xyzzy
xyzzy

C:\Pax> <nul: set /p junk=" xyzzy"
xyzzy

C:\Pax> <nul: set /p junk=' xyzzy'
' xyzzy'

C:\Pax> <nul: set /p junk=. xyzzy
. xyzzy

C:\Pax> <nul: set /p junk=^ xyzzy
xyzzy

What I need is:

C:\Pax> some_magical_command_with_an_argument xyzzy
 xyzzy

which will give me the space(s) at the start and no newline at the end.

解决方案

This isn't using set or other cmd-internal stuff, but you can use cscript (also included in a standard Windows install) to do a similar thing. You can even control it from the cmd file itself (no separate files to maintain) by use of creating temporary vbs files.

Place this code in your cmd file:

rem Create the VBS file to output spaces and a word.

echo.for i = 1 to WScript.Arguments.Item(0) >spacetext.vbs
echo.     WScript.StdOut.Write ^" ^" >>spacetext.vbs
echo.next >>spacetext.vbs
echo.WScript.StdOut.Write WScript.Arguments.Item(1) >>spacetext.vbs

rem Do this once per word you want output (eg, in a loop).

cscript /nologo spacetext.vbs 0 Hello,
cscript /nologo spacetext.vbs 1 my
cscript /nologo spacetext.vbs 1 name
cscript /nologo spacetext.vbs 1 is
cscript /nologo spacetext.vbs 4 Pax.

rem Do this at the end to add newline and kill temp file.

echo.
del spacetext.vbs

The output of this is what you would expect:

Hello, my name is    Pax.

这篇关于Windows等效的“echo -n”不再工作在Win7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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