在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
当前回答
Late answer here, but for anyone who needs to write special characters to a single line who find dbenham's answer to be about 80 lines too long and whose scripts may break (perhaps due to user-input) under the limitations of simply using set /p, it's probably easiest to just to pair your .bat or .cmd with a compiled C++ or C-language executable and then just cout or printf the characters. This will also allow you to easily write multiple times to one line if you're showing a sort of progress bar or something using characters, as OP apparently was.
其他回答
你可以从gnuwin32 (coreutils包)中使用“tr”删除换行符
@echo off
set L=First line
echo %L% | tr -d "\r\n"
echo Second line
pause
顺便说一下,如果您正在编写大量脚本,gnuwin32是一个金矿。
jscript:
@if (@X)==(@Y) @end /*
@cscript //E:JScript //nologo "%~nx0" %*
@exit /b %errorlevel%
*/if(WScript.Arguments.Count()>0) WScript.StdOut.Write(WScript.Arguments.Item(0));
如果它叫write.bat,你可以这样测试它:
call write.bat string & echo _Another_String_
如果你想使用powershell,但有cmd定义的变量,你可以使用:
set str=_My_StrinG_
powershell "Write-Host -NoNewline ""%str%"""" & echo #Another#STRING#
DIY cw.exe(控制台编写)实用程序
如果你找不到现成的,你可以自己动手做。有了这个cw实用程序,你可以使用每一种字符。至少,我是这么想的。请对它进行压力测试,然后告诉我。
工具
你所需要的只是安装。net,这在当今非常普遍。
材料
输入/复制粘贴了一些字符。
步骤
用以下内容创建.bat文件。
/* >nul 2>&1
@echo off
setlocal
set exe=cw
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*csc.exe"') do set "csc=%%v"
"%csc%" -nologo -out:"%exe%.exe" "%~f0"
endlocal
exit /b %errorlevel%
*/
using System;
namespace cw {
class Program {
static void Main() {
var exe = Environment.GetCommandLineArgs()[0];
var rawCmd = Environment.CommandLine;
var line = rawCmd.Remove(rawCmd.IndexOf(exe),exe.Length).TrimStart('"');
line = line.Length < 2 ? "\r" : line.Substring(2) ;
Console.Write(line);
}
}
}
运行它。 现在您有了一个很棒的4KB实用程序,可以删除.bat。
或者,您可以在任何批处理中插入此代码作为子例程,将生成的.exe发送到%temp%,在批处理中使用它,并在完成时删除它。
如何使用
如果你想写一些没有换行的东西: 任何你想要的,即使是"",但记住转义^|,^^,^&等,除非双引号,如"| ^& "。
如果你想要一个回车(回到行首),运行just 连续波
所以试试从命令行:
for /l %a in (1,1,1000) do @(cw ^|&cw&cw /&cw&cw -&cw&cw \&cw)
使用:echo | set /p=或<NUL set /p=都可以抑制换行符。
然而,当编写更高级的脚本时,检查ERRORLEVEL变得很重要,因为设置set /p=而不指定变量名将把ERRORLEVEL设置为1,这可能是非常危险的。
一个更好的方法是使用一个虚拟变量名,就像这样: echo | set /p dummyName=Hello World
这将产生你想要的东西,没有任何偷偷摸摸的东西在后台,因为我必须找到艰难的方法,但这只适用于管道版本;<NUL set /p dummyName=Hello仍然会将ERRORLEVEL提升为1。
使用set和/p参数可以不带换行符进行回显:
C:\> echo Hello World
Hello World
C:\> echo|set /p="Hello World"
Hello World
C:\>
源