在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
当前回答
从这里
<nul set /p =Testing testing
同时也从空间的使用开始呼应
echo.Message goes here
其他回答
可以使用set /p命令禁用新行。set /p命令不识别空格,因此可以使用点和退格字符使其识别空格。您还可以使用变量作为内存,并将想要打印的内容存储在其中,这样您就可以打印变量而不是语句。例如:
@echo off
setlocal enabledelayedexpansion
for /f %%a in ('"prompt $H & for %%b in (1) do rem"') do (set "bs=%%a")
cls
set "var=Hello World! :)"
set "x=0"
:loop
set "display=!var:~%x%,1!"
<nul set /p "print=.%bs%%display%"
ping -n 1 localhost >nul
set /a "x=%x% + 1"
if "!var:~%x%,1!" == "" goto end
goto loop
:end
echo.
pause
exit
通过这种方式,您可以打印任何内容而无需新行。我已经使程序打印字符一个接一个,但你也可以使用文字,而不是通过改变循环字符。
在上面的例子中,我使用了" enabledelayeexpand ",所以set /p命令不识别"!"字符,并打印一个点来代替它。我希望你不要用感叹号“!”;)
使用:echo | set /p=或<NUL set /p=都可以抑制换行符。
然而,当编写更高级的脚本时,检查ERRORLEVEL变得很重要,因为设置set /p=而不指定变量名将把ERRORLEVEL设置为1,这可能是非常危险的。
一个更好的方法是使用一个虚拟变量名,就像这样: echo | set /p dummyName=Hello World
这将产生你想要的东西,没有任何偷偷摸摸的东西在后台,因为我必须找到艰难的方法,但这只适用于管道版本;<NUL set /p dummyName=Hello仍然会将ERRORLEVEL提升为1。
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是一个金矿。
受到这个问题答案的启发,我制作了一个简单的计数器批处理脚本,在同一行上打印进度值(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