一个 echo 命令中有两个换行符

发布于 2024-10-12 07:55:27 字数 588 浏览 3 评论 0原文

简而言之,我在 Windows XP/Windows 7 批处理文件中设置了一个最基本的文本进度指示器,该指示器在命令行工具工作时的输出之后写入,因为它看起来暂时没有执行任何操作。我通过在命令后传递简介来实现这一点,如下所示:

app.exe -args "file"|echo Writing "file"...

这会产生如下所示的结果:

_______________
|app for work |  
|ver:2 10/2009|  
|_____________|  
Writing "file"...

有没有办法在应用程序的输出和我的输出之间插入换行符?

我做过但失败的事情:

|echo.|echo
|echo.|echo.|echo
|echo.&echo.&echo
|echo [alt+255/hex:FF/ÿ/EOL][same again]
|echo -e \r\r (lol)
|echo.. (nope!)

& 可能会延迟到应用程序执行其操作之后,到那时就为时已晚......我错过了什么吗?有可能吗?我知道这并不重要,但我很好奇。

Simply put, I've set a most basic text progress indicator in a Windows XP/Windows 7 batch file that writes just after a command line tool's output while it works, because it doesn't look like it does anything for a bit. I do so by piping the blurb after the command as such:

app.exe -args "file"|echo Writing "file"...

This results in something like the following:

_______________
|app for work |  
|ver:2 10/2009|  
|_____________|  
Writing "file"...

Is there a way to slip a newline in between the application's output and my output?

Things I've done and failed:

|echo.|echo
|echo.|echo.|echo
|echo.&echo.&echo
|echo [alt+255/hex:FF/ÿ/EOL][same again]
|echo -e \r\r (lol)
|echo.. (nope!)

The &'s are probably delayed until after the application does its thing, and by then it's too late... Have I missed something? Is it even possible? I know this is not truly important, but I'm very curious.

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

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

发布评论

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

评论(4

赠意 2024-10-19 07:55:27

从您的示例中,我怀疑 app.exe 实际上将其输出发送到标准错误,而不是标准输出。否则,它的输出将被发送到管道,并且 echo 会吞噬它。

这个怎么样:

app.exe -args "file" | (echo. & echo Writing "file")

From your example, I suspect that app.exe actually sends its output to standard error, rather than standard output. Otherwise its output would have been sent to the pipe and echo would have gobbled it up.

How about this:

app.exe -args "file" | (echo. & echo Writing "file")
椵侞 2024-10-19 07:55:27

好的,就您而言,存在多个问题。

通常,您可以通过一个小技巧来回显换行,

setlocal EnableDelayedExpansion
set LF=^


rem Two empty lines are neccessary for the LF creation
rem In the variable <lf> is one line feed character
echo Line1!lf!Line2
------ OUTPUT -----
Line1
Line2

但在您的情况下,这不起作用,因为管道破坏了延迟扩展。

但是管道会产生更多问题,因为它启动两个异步任务。

实际上,您的 app.exe 作为任务 1 运行,而 echo 写入文件 作为任务 2 运行。
因此,任务 2 可能会在任务 1 之前执行,输出将是

app.exe -args "file" | (echo. & echo Writing "file")

Writing "file"...
_______________
|app for work |
|ver:2 10/2009|
|_____________|

_______________
|app for work |
|ver:2 10/2009|
|_____________|

Writing "file"...

可以测试它(在命令行上)

echo one >&2 | echo two

在大多数情况下,输出是:

one
two

但并非总是如此!

Ok, in your case, there are multiple problems.

Normally you can echo a line feed with a small trick

setlocal EnableDelayedExpansion
set LF=^


rem Two empty lines are neccessary for the LF creation
rem In the variable <lf> is one line feed character
echo Line1!lf!Line2
------ OUTPUT -----
Line1
Line2

But in your case this doesn't work, because the pipe breaks the delayed expansion.

But the pipe will make more problems, as it starts two asynchronous tasks.

In reallity your app.exe runs as task1 and echo Writing file runs as task2.
So it can happen that task2 will be executed before task1, the output would be

app.exe -args "file" | (echo. & echo Writing "file")

Writing "file"...
_______________
|app for work |
|ver:2 10/2009|
|_____________|

instead of

_______________
|app for work |
|ver:2 10/2009|
|_____________|

Writing "file"...

You can test it (on the cmdline)

echo one >&2 | echo two

In the most cases the output is:

one
two

But not always!

和影子一齐双人舞 2024-10-19 07:55:27

您可能想要分组您的两个 echo 命令:

app.exe -args "file" & ( echo ""; echo Writing "file" )

you might want to group your two echo commands:

app.exe -args "file" & ( echo ""; echo Writing "file" )
沒落の蓅哖 2024-10-19 07:55:27

怎么样

echo -e "`app.exe -args \"file\"` \n Writing file"

What about

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