在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
当前回答
受到这个问题答案的启发,我制作了一个简单的计数器批处理脚本,在同一行上打印进度值(0-100%)(覆盖前一行)。也许这对寻找类似解决方案的其他人也有价值。
注:*为不可打印字符,应使用[Alt + Numpad 0 + Numpad 8]组合键输入,即退格字符。
@ECHO OFF
FOR /L %%A in (0, 10, 100) DO (
ECHO|SET /P="****%%A%%"
CALL:Wait 1
)
GOTO:EOF
:Wait
SET /A "delay=%~1+1"
CALL PING 127.0.0.1 -n %delay% > NUL
GOTO:EOF
其他回答
这里是另一个方法,它使用Powershell Write-Host,它有一个-NoNewLine参数,结合start /b,它从批处理中提供了相同的功能。
NoNewLines.cmd
@ECHO OFF
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 1 - ';Write-Host -NoNewLine 'Result 2 - ';Write-Host -NoNewLine 'Result 3 - '"
PAUSE
输出
Result 1 - Result 2 - Result 3 - Press any key to continue . . .
下面这个略有不同,并不完全像OP想要的那样工作,但很有趣,因为每个结果都覆盖了模拟计数器的前一个结果。
@ECHO OFF
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 1 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 2 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 3 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 4 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 5 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 6 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 7 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 8 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 9 - '"
PAUSE
简单的SET /P方法在不同的Windows版本之间有一些限制。
前导引号可能被删除 前导空白可能被剥离 Leading =导致语法错误。
更多信息请参见http://www.dostips.com/forum/viewtopic.php?f=3&t=4209。
jeb发布了一个聪明的解决方案,解决了输出文本没有换行的大部分问题,甚至有前导空格或=我已经改进了方法,以便它可以安全地打印任何有效的批处理字符串,没有新行,在任何版本的Windows XP开始。注意:writeInitialize方法包含一个字符串字面值,可能不能很好地发布到站点。包括一个注释,描述字符序列应该是什么。
:write和:writeVar方法经过优化,只有包含麻烦的前导字符的字符串才会使用我修改过的jeb的COPY方法来编写。不麻烦的字符串是使用更简单和更快的SET /P方法编写的。
@echo off
setlocal disableDelayedExpansion
call :writeInitialize
call :write "=hello"
call :write " world!%$write.sub%OK!"
echo(
setlocal enableDelayedExpansion
set lf=^
set "str= hello!lf!world^!!!$write.sub!hello!lf!world"
echo(
echo str=!str!
echo(
call :write "str="
call :writeVar str
echo(
exit /b
:write Str
::
:: Write the literal string Str to stdout without a terminating
:: carriage return or line feed. Enclosing quotes are stripped.
::
:: This routine works by calling :writeVar
::
setlocal disableDelayedExpansion
set "str=%~1"
call :writeVar str
exit /b
:writeVar StrVar
::
:: Writes the value of variable StrVar to stdout without a terminating
:: carriage return or line feed.
::
:: The routine relies on variables defined by :writeInitialize. If the
:: variables are not yet defined, then it calls :writeInitialize to
:: temporarily define them. Performance can be improved by explicitly
:: calling :writeInitialize once before the first call to :writeVar
::
if not defined %~1 exit /b
setlocal enableDelayedExpansion
if not defined $write.sub call :writeInitialize
set $write.special=1
if "!%~1:~0,1!" equ "^!" set "$write.special="
for /f delims^=^ eol^= %%A in ("!%~1:~0,1!") do (
if "%%A" neq "=" if "!$write.problemChars:%%A=!" equ "!$write.problemChars!" set "$write.special="
)
if not defined $write.special (
<nul set /p "=!%~1!"
exit /b
)
>"%$write.temp%_1.txt" (echo !str!!$write.sub!)
copy "%$write.temp%_1.txt" /a "%$write.temp%_2.txt" /b >nul
type "%$write.temp%_2.txt"
del "%$write.temp%_1.txt" "%$write.temp%_2.txt"
set "str2=!str:*%$write.sub%=%$write.sub%!"
if "!str2!" neq "!str!" <nul set /p "=!str2!"
exit /b
:writeInitialize
::
:: Defines 3 variables needed by the :write and :writeVar routines
::
:: $write.temp - specifies a base path for temporary files
::
:: $write.sub - contains the SUB character, also known as <CTRL-Z> or 0x1A
::
:: $write.problemChars - list of characters that cause problems for SET /P
:: <carriageReturn> <formFeed> <space> <tab> <0xFF> <equal> <quote>
:: Note that <lineFeed> and <equal> also causes problems, but are handled elsewhere
::
set "$write.temp=%temp%\writeTemp%random%"
copy nul "%$write.temp%.txt" /a >nul
for /f "usebackq" %%A in ("%$write.temp%.txt") do set "$write.sub=%%A"
del "%$write.temp%.txt"
for /f %%A in ('copy /z "%~f0" nul') do for /f %%B in ('cls') do (
set "$write.problemChars=%%A%%B ""
REM the characters after %%B above should be <space> <tab> <0xFF>
)
exit /b
示例1:它工作并生成退出代码= 0。这很好。 注意“。”,直接在echo之后。
C:\Users\phife.狗\ gitrepos \ 1 # \ repo_abc \脚本 @echo。| set /p JUNK_VAR=这是一个像Linux echo一样显示的消息-n将显示它…&回显%ERRORLEVEL%
这是一个像Linux echo -n一样显示的消息…0
示例2:这可以工作,但产生退出代码= 1。这很糟糕。 请注意回声后面缺少“。”。这似乎就是不同之处。
C:\Users\phife.狗\ gitrepos \ 1 # \ repo_abc \脚本 @echo | set /p JUNK_VAR=这是一个像Linux echo -n会显示它的消息…&回显%ERRORLEVEL%
这是一个像Linux echo -n一样显示的消息…1
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#
你可以从gnuwin32 (coreutils包)中使用“tr”删除换行符
@echo off
set L=First line
echo %L% | tr -d "\r\n"
echo Second line
pause
顺便说一下,如果您正在编写大量脚本,gnuwin32是一个金矿。