Windows命令行 - 尝试在同一行中运行2个命令时错误

发布于 2025-02-08 19:56:47 字数 1151 浏览 2 评论 0原文

我有一个.bat文件正常工作:

set list=1 5 10 18 22 27
for %%p in (%list%) do @ping 192.168.0.%%p -w 10 -n 1 | findstr "Reply Request"

当我尝试在线运行它时,与& && 在这样的一行中结合使用

set list=1 5 10 18 22 27 && for %%p in (%list%) do @ping 192.168.0.%%p -w 10 -n 1 | findstr "Reply Request"

。说:

%%p was unexpected at this time.

我在做什么错?

如果组合命令在BAT文件中确实有效,但是如果粘贴在命令行中。

这样做的目的是让Excel生成一行复制命令,以在命令提示符中运行,因为创建批处理文件不允许在远程系统上。

解决: 谢谢Magoo和Stephan! 解决方案是包围数组:

set "list=1 5 10 18 22 27" 

并使用%p,如果%% P:

for %p in (%list%) do @ping 192.168.0.%p -w 10 -n 1 | findstr "Reply Request"

最终结果:

set "list=1 5 10 18 22 27" && for %p in (%list%) do @ping 192.168.0.%p -w 10 -n 1 | findstr "Reply Request"

:)

ps: magoo建议更简化的解决方案:

for %p in (1 5 10 18 22 27) do @ping 192.168.0.%p -w 10 -n 1 | findstr "Reply Request"

简单,单个命令:)

I have a .bat file that works properly:

set list=1 5 10 18 22 27
for %%p in (%list%) do @ping 192.168.0.%%p -w 10 -n 1 | findstr "Reply Request"

When I try to run it inline, combined with & or && in one line like this

set list=1 5 10 18 22 27 && for %%p in (%list%) do @ping 192.168.0.%%p -w 10 -n 1 | findstr "Reply Request"

I get an error saying:

%%p was unexpected at this time.

What am I doing wrong?

If the combined command is in the bat file it does work, but not if pasted in the command line.

The purpose of this is to have excel generate one line copy-paste command to be run in the command prompt, as creation the batch file is not allowed on the remote system.

SOLVED:
Thank you Magoo and Stephan!
The solution is to enclose array:

set "list=1 5 10 18 22 27" 

And to use %p instead if %%p:

for %p in (%list%) do @ping 192.168.0.%p -w 10 -n 1 | findstr "Reply Request"

So final result:

set "list=1 5 10 18 22 27" && for %p in (%list%) do @ping 192.168.0.%p -w 10 -n 1 | findstr "Reply Request"

does work :)

PS: Magoo suggested more streamlined solution:

for %p in (1 5 10 18 22 27) do @ping 192.168.0.%p -w 10 -n 1 | findstr "Reply Request"

Simple, single command :)

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

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

发布评论

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

评论(2

南城旧梦 2025-02-15 19:56:47

list解析行时的值是不确定的,因此> batch替换 nother> nothing list list) list%和执行的解决结果是

set list=1 5 10 18 22 27 && for %%p in () do @ping 192.168.0.%%p -w 10...

如此,

for %p in (1 5 10 18 22 27) do @ping 192.168.0.%p -w 10...

使用设置“ var = value”用于设置字符串值 - 这避免了落后空间引起的问题。不要分配终端后斜线, space - 从元素构建路径名 - 违反直觉,它可能会使过程更容易。

The value of list is undefined when the line is parsed, so batch substitutes nothing (the "contents" of list) for %list% and the resolved result that is executed is

set list=1 5 10 18 22 27 && for %%p in () do @ping 192.168.0.%%p -w 10...

So, what's wrong with

for %p in (1 5 10 18 22 27) do @ping 192.168.0.%p -w 10...

Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal backslash, Space or " - build pathnames from the elements - counterintuitively, it is likely to make the process easier.

在你怀里撒娇 2025-02-15 19:56:47

要使用在同一行(或代码块)中定义的变量,您需要延迟扩展

默认情况下禁用了延迟的扩展,因此您需要在启用延迟扩展的情况下启动cmd的新实例:(

cmd /v:on /c "set "list=1 5 10 18 22 27" & for %p in (!list!) do @ping 192.168.0.%p -w 10 -n 1 | findstr "Reply Request""

虽然我更喜欢Magoo的解决方案)

To use a variable defined in the same line (or code block), you need delayed expansion

Delayed expansion is disabled by default, so you need to start a new instance of cmd with delayed expansion enabled:

cmd /v:on /c "set "list=1 5 10 18 22 27" & for %p in (!list!) do @ping 192.168.0.%p -w 10 -n 1 | findstr "Reply Request""

(Although I'd prefer Magoo's solution)

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