gre*_*eth 213 windows cmd newline batch-file
什么是Linux shell命令相当于echo -n在输出结尾处抑制换行符的Linux shell命令?
想法是在循环内写入同一行.
arn*_*nep 222
使用set和/p参数可以在没有换行符的情况下回显:
C:\> echo Hello World
Hello World
C:\> echo|set /p="Hello World"
Hello World
C:\>
Run Code Online (Sandbox Code Playgroud)
小智 96
使用:echo | set /p=或者<NUL set /p=都可以抑制换行.
但是,在检查ERRORLEVEL变得很重要时编写更高级的脚本会非常危险,因为设置set /p=而不指定变量名称会将ERRORLEVEL设置为1.
更好的方法是使用虚拟变量名称,如下所示:
echo | set /p dummyName=Hello World
这将产生你想要的东西而没有任何偷偷摸摸的东西在后台发生,因为我必须找到困难的方式,但这只适用于管道版本; <NUL set /p dummyName=Hello仍会将ERRORLEVEL提高到1.
dbe*_*ham 27
简单的SET/P方法具有Windows版本之间略有不同的限制.
领先的报价可能会被剥夺
领先的白色空间可能会被剥离
前导=导致语法错误.
有关详细信息,请参阅http://www.dostips.com/forum/viewtopic.php?f=3&t=4209.
jeb发布了一个聪明的解决方案,解决了没有换行的输出文本中的大多数问题,即使是前导空格或=我已经改进了方法,以便它可以在任何版本的Windows上安全地打印绝对任何有效的批处理字符串而无需新行从XP开始.请注意,该:writeInitialize方法包含一个字符串文字,可能无法很好地发布到网站.包括描述字符序列应该是什么的注释.
该:write和:writeVar方法进行了优化,使得只有含麻烦的前导字符的字符串都使用我的杰布的复制方法的修改后的版本写的.使用更简单,更快速的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
Run Code Online (Sandbox Code Playgroud)
小智 10
作为@ xmechanix答案的附录,我注意到通过将内容写入文件:
echo | set /p dummyName=Hello World > somefile.txt
Run Code Online (Sandbox Code Playgroud)
这将在打印字符串的末尾添加额外的空间,这可能不方便,特别是因为我们试图避免在字符串的末尾添加新行(另一个空白字符).
幸运的是,引用要打印的字符串,即使用:
echo | set /p dummyName="Hello World" > somefile.txt
Run Code Online (Sandbox Code Playgroud)
将打印字符串,最后没有任何换行符或空格字符.
SET/P中剥离的空白区域的解决方案:
诀窍是退格char,你可以在文本编辑器EDIT for DOS中召唤.要在EDIT中创建它,请按ctrlP + ctrlH.我会把它粘贴在这里但是这个网页无法显示它.它虽然在记事本上可见(但它很像,一个黑色的小矩形,中间有一个白色的圆圈)
所以你写这个:
<nul set /p=.9 Hello everyone
Run Code Online (Sandbox Code Playgroud)
点可以是任何字符,它只是告诉SET/P文本从那里开始,在空格之前,而不是在" 你好 "." 9 "表示我无法在此显示的退格字符.你必须把它代替9,它将删除" . ",之后你会得到这个:
Hello Everyone
Run Code Online (Sandbox Code Playgroud)
代替:
Hello Everyone
Run Code Online (Sandbox Code Playgroud)
我希望它有所帮助
我根据 @arnep 的想法创建了一个函数:
echo|set /p="你好世界"
这里是:
:SL (sameline)
echo|set /p=%1
exit /b
Run Code Online (Sandbox Code Playgroud)
使用它call :SL "Hello There"
我知道这没什么特别的,但我花了很长时间才想到它,我想我应该把它发布在这里。
您可以使用gnuwin32(coreutils包)中的"tr"删除换行符
@echo off
set L=First line
echo %L% | tr -d "\r\n"
echo Second line
pause
Run Code Online (Sandbox Code Playgroud)
顺便说一句,如果你正在做大量的脚本编写,gnuwin32就是一个金矿.
这是另一种方法,它使用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
Run Code Online (Sandbox Code Playgroud)
产量
Result 1 - Result 2 - Result 3 - Press any key to continue . . .
Run Code Online (Sandbox Code Playgroud)
下面的这个稍微不同,不像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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
226377 次 |
| 最近记录: |