在同一条线上呼应

发布于 2024-09-30 18:27:46 字数 212 浏览 0 评论 0原文

我查看了您数据库之前的问题,我没有尝试回答,但我尝试了。

我想编写以下几行代码:

echo Executing backup....
backup procedure
echo Ok

但输出应该是:

Executing backup... Ok

这可能吗?!

I had a look at the previous questions of your db and I didn't try an answer, but I try.

I would like to write the following lines code:

echo Executing backup....
backup procedure
echo Ok

but the output should be:

Executing backup... Ok

That's possible?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

好久不见√ 2024-10-07 18:27:46

我想你正在使用 dos/nt-batch。

可以使用 set /p 命令,因为 set /p 不会打印 CrLf

set /p "=Executing backup...." <nul
echo OK

也可以使用 CR 字符擦除该行。
重要的是要知道一组 /p 前面的空白字符会被忽略(在 Vista 中,而不是在 XP 中),因此 !cr!必须放在后面或最后。
CR只能用delayedExpansion来显示,因为%cr%可以工作,但是CR字符在百分比扩展阶段(或直接在此阶段之后)被删除,但在延迟扩展阶段不会被删除。

仅使用一行显示的计数器示例

@echo off
setlocal EnableDelayedExpansion EnableExtensions


call :CreateCR
for /l %%n in (1,1,10000) do (
    set /P "=Count %%n!CR!" <nul
)
echo(
goto :eof

:CreateCR
rem setlocal EnableDelayedExpansion EnableExtensions
set "X=."
for /L %%c in (1,1,13) DO set X=!X:~0,4094!!X:~0,4094!

echo !X!  > %temp%\cr.tmp
echo\>> %temp%\cr.tmp
for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do (
   endlocal
   set cr=%%a
   goto :eof
)
goto :eof

编辑:解释,如何创建变量cr(通过技巧完成)

设置变量X 变为单个点(字符本身不重要),通过 for /L %%c in (1,1,13) DO set X=!X:~0 重复成为 8188 个字符,4094!!X:~0,4094!

然后将变量、两个空格以及 CR 和 LF 回显到文件中 echo !X! > %temp%\cr.tmp (注意 !X!> 之间的两个空格以及自然行尾 echo 内部修正)

我们现在有 8192 个字符,但数据缓冲区只能容纳 8191 个字符,因此最后一个字符(换行符)将被删除!

在下一行中 echo\>> %temp%\cr.tmp,另一个 CR/LF 组被追加到文件中(命令中的 \ 只是不输出任何内容,除了回车符返回和换行,因为 echo 本身会输出 ECHO is ON/OFF),这一点很重要,因为单个 CR 不能在行尾读取(稍后详细介绍)。

因此该文件现在包含 <8188 .'s>

for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do 读取第二个标记,分隔符是标准空格和制表符,因此第二个标记只是一个 CR,因为以下 CR/LF 已作为标准行结尾被删除。

最后,endlocal 用于返回到不存在临时变量 Xca 的环境(如将 endlocal 放在括号中,它允许在 endlocal 在括号末尾实际生效之前设置 cr(也可以写成as for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do endlocal&set cr=%%a&goto :eof)

另外

这是我创建 CR 角色的第一种方法,但它需要一些时间和一个临时文件。
后来我看到了一种从 copy /z 命令检索 CR 的更简单方法。

for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

I suppose you are using dos/nt-batch.

It is possible with the set /p command, because set /p doesn't print a CrLf

set /p "=Executing backup...." <nul
echo OK

Also it's possible to erase the line with a CR character.
It's important to know that whitespace characters at the front of an set /p are ignored (in Vista, not in XP), so the !cr! has to placed later or at the end.
A CR can only be displayed with delayedExpansion, because %cr% works, but CR characters are removed in the percent expansion phase(or directly after this phase), but not in the delayed expansion phase.

Example of a counter which use only one line for displaying

@echo off
setlocal EnableDelayedExpansion EnableExtensions


call :CreateCR
for /l %%n in (1,1,10000) do (
    set /P "=Count %%n!CR!" <nul
)
echo(
goto :eof

:CreateCR
rem setlocal EnableDelayedExpansion EnableExtensions
set "X=."
for /L %%c in (1,1,13) DO set X=!X:~0,4094!!X:~0,4094!

echo !X!  > %temp%\cr.tmp
echo\>> %temp%\cr.tmp
for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do (
   endlocal
   set cr=%%a
   goto :eof
)
goto :eof

EDIT: Explanation, how the variable cr is created (Done with a trick)

After setting variable X to a single dot (the character itself is unimportant), it is repeated to become 8188 characters by way of for /L %%c in (1,1,13) DO set X=!X:~0,4094!!X:~0,4094!

Then the variable, two spaces and both a CR and LF are echoed into a file with echo !X! > %temp%\cr.tmp (Notice the two spaces between the !X! and the > and the natural line endings echo amends internally)

We now have 8192 characters, but the data buffer can only hold 8191 characters, so the last character (the linefeed) will be dropped!

In the next line echo\>> %temp%\cr.tmp, another CR/LF set is appended to the file (the \ in the command is just to output nothing bar the carriage return and line feed, as echo by it's self will output ECHO is ON/OFF), that's important, as a single CR can't be read at the end of a line (More later).

So the file now contains <8188 .'s><SPACE><SPACE><CR><CR><LF>

The for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do reads the second token, the delimters are standard space and tab, so the second token is only a single CR, as the following CR/LF is removed as standard line ending.

Finally the endlocal is used to return to an environment without the temporary variables X, c and a existing (As with the endlocal in brackets, it allows the setting of cr before the endlocal actually takes affect at the end of the brackets (could also be written as for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do endlocal&set cr=%%a&goto :eof)

Additionally

This was my first way to create a CR character, but it needs some time and a temporary file.
Later I saw a simpler method of retrieving the CR from a copy /z command.

for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
相思碎 2024-10-07 18:27:46

在 Posix 系统 (Linux) 上尝试一下,

echo -n "Executing backup.... "
echo -n "backup procedure "
echo "Ok"

在 Windows 上则困难得多。您将需要使用如下内容:

@echo off
echo|set /p ="Executing backup...."
echo|set /p =" backup procedure"

检查此帖子:http://www.pcreview.co.uk/forums/showpost.php?s=1a20b16775d915998b30bd76a0ec5d35&p=4432915&postcount=7

Try this on Posix system (Linux)

echo -n "Executing backup.... "
echo -n "backup procedure "
echo "Ok"

It is much harder on Windows. You will need to use something like this:

@echo off
echo|set /p ="Executing backup...."
echo|set /p =" backup procedure"

Check this post: http://www.pcreview.co.uk/forums/showpost.php?s=1a20b16775d915998b30bd76a0ec5d35&p=4432915&postcount=7.

月亮邮递员 2024-10-07 18:27:46

这有点黑客,但这里有一个 描述如何在 Windows 上执行此操作的文章

从文章中来看,最终结果(根据您的设置进行编辑)如下所示:

SET /P var=Backing up
%Result%....<NUL

Backup_process %Result% >NUL 2>&1

IF ERRORLEVEL 1 
  ECHO FAIL 
ELSE
  ECHO OK

It's a bit of a hack, but here is an article describing how to do it for Windows.

From the article, the final result (edited for your setup) looks like this:

SET /P var=Backing up
%Result%....<NUL

Backup_process %Result% >NUL 2>&1

IF ERRORLEVEL 1 
  ECHO FAIL 
ELSE
  ECHO OK
淡墨 2024-10-07 18:27:46

我用 VBScript 做了类似的事情。

将此代码放入 EchoNoNewline.vbs 中:

If WScript.Arguments.Named.Exists("TEXT") Then
  WScript.StdOut.Write WScript.Arguments.Named.Item("TEXT")
End If

从批处理文件中,使用如下脚本:

CSCRIPT EchoNoNewLine.vbs //NOLOGO /TEXT:"Executing backup...."
backup procedure
CSCRIPT EchoNoNewLine.vbs //NOLOGO /TEXT:"Ok"

I've done something similar using a VBScript.

Put this code in EchoNoNewline.vbs:

If WScript.Arguments.Named.Exists("TEXT") Then
  WScript.StdOut.Write WScript.Arguments.Named.Item("TEXT")
End If

From your batch file, use the script like this:

CSCRIPT EchoNoNewLine.vbs //NOLOGO /TEXT:"Executing backup...."
backup procedure
CSCRIPT EchoNoNewLine.vbs //NOLOGO /TEXT:"Ok"
多情出卖 2024-10-07 18:27:46

cmd 中管道之前的正斜杠如何删除 echo 的行结尾?
最好的建议是:

在没有换行的情况下回显文本的效率非常低,因为管道会创建两个新的 cmd.exe 实例。
使用起来更加简单快捷

     <nul set /p "=My Text"

从 NUL 重定向也会停止等待用户输入。

at What does a forward slash before a pipe in cmd do to remove the line ending of an echo?
the best suggestion is:

to echo text without a linefeed is very inefficient, as a pipe creates two new instances of cmd.exe.
It's much simpler and faster to use

     <nul set /p "=My Text"

The redirect from NUL will also stop the waiting for user input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文