加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

相当于“echo -n”的Windows不再适用于Win7

发布时间:2020-12-14 02:45:52 所属栏目:Windows 来源:网络整理
导读:我在 Windows cmd.exe(至少高达XP)中有一个漂亮的技巧来模拟UNIX echo的行为而没有换行符echo -n.例如,命令: nul: set /p junk= xyzzy 将导致正好六个字符输出,前导空格和字符串“xyzzy”,没有别的. If you’re interested in why this works,it’s actuall
我在 Windows cmd.exe(至少高达XP)中有一个漂亮的技巧来模拟UNIX echo的行为而没有换行符echo -n.例如,命令:

<nul: set /p junk= xyzzy

将导致正好六个字符输出,前导空格和字符串“xyzzy”,没有别的.

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.

在(例如)处理循环中的文件(每个文件一次迭代)时,它在cmd脚本中非常有用,您希望每行列出多个文件.通过使用这个技巧,您可以简单地输出每个文件名后跟一个空格而不是换行符,然后在循环之后输出换行符来完成:

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

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

Processing files:
file1.txtfile42.txtp0rn.zip

有没有办法我可以设置/ 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

这将在开始时给我空间,最后没有新行.

解决方法

这与paxdiablo的答案非常相似,除了我使用混合JScript /批处理文件而不是临时VBScript文件.

我的脚本名为jEval.bat – 它只是评估任何有效的JScript表达式,并将结果写入stdout,可选择使用尾随换行符.这个愚蠢的小脚本对于批量编程非常有用.

假设jEval.bat位于当前文件夹中,或者位于PATH中的某个位置,那么您可以执行以下操作:

call jeval "' xyzzy'"

这是脚本.这真的很简单.大多数代码都与文档,错误处理和内置帮助系统有关.

@if (@X)==(@Y) @end /* harmless hybrid line that begins a JScrpt comment

::************ Documentation ***********
:::
:::jEval  JScriptExpression  [/N]
:::jEval  /?
:::
:::  Evaluates a JScript expression and writes the result to stdout.
:::
:::  A newline (CR/LF) is not appended to the result unless the /N
:::  option is used.
:::
:::  The JScript expression should be enclosed in double quotes.
:::
:::  JScript string literals within the expression should be enclosed
:::  in single quotes.
:::
:::  Example:
:::
:::    call jEval "'5/4 = ' + 5/4"
:::
:::  Output:
:::
:::    5/4 = 1.25
:::

::************ Batch portion ***********
@echo off

if "%~1" equ "" (
  call :err "Insufficient arguments"
  exit /b
)
if "%~2" neq "" if /i "%~2" neq "/N" (
  call :err "Invalid option"
  exit /b
)
if "%~1" equ "/?" (
  setlocal enableDelayedExpansion
  for /f "delims=" %%A in ('findstr "^:::" "%~f0"') do (
    set "ln=%%A"
    echo(!ln:~3!
  )
  exit /b
)
cscript //E:JScript //nologo "%~f0" %*
exit /b

:err
>&2 echo ERROR: %~1. Use jeval /? to get help.
exit /b 1


************ JScript portion ***********/
if (WScript.Arguments.Named.Exists("n")) {
  WScript.StdOut.WriteLine(eval(WScript.Arguments.Unnamed(0)));
} else {
  WScript.StdOut.Write(eval(WScript.Arguments.Unnamed(0)));
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读