批处理等效于 Bash 反引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2768608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch equivalent of Bash backticks
提问by MiffTheFox
When working with Bash, I can put the output of one command into another command like so:
使用 Bash 时,我可以将一个命令的输出放入另一个命令中,如下所示:
my_command `echo Test`
would be the same thing as
将与
my_command Test
(Obviously, this is just a non-practical example.)
(显然,这只是一个不实际的例子。)
I'm just wondering if you can do the same thing in Batch.
我只是想知道你是否可以在 Batch 中做同样的事情。
采纳答案by zvrba
You can do it by redirecting the output to a file first. For example:
您可以通过首先将输出重定向到文件来实现。例如:
echo zz > bla.txt
set /p VV=<bla.txt
echo %VV%
回答by Michael Burr
You can get a similar functionality using cmd.exe scripts with the for /f
command:
您可以通过以下for /f
命令使用 cmd.exe 脚本获得类似的功能:
for /f "usebackq tokens=*" %%a in (`echo Test`) do my_command %%a
Yeah, it's kinda non-obvious (to say the least), but it's what's there.
是的,它有点不明显(至少可以说),但它就是存在的。
See for /?
for the gory details.
查看for /?
血腥细节。
Sidenote: I thought that to use "echo
" inside the backticks in a "for /f
" command would need to be done using "cmd.exe /c echo Test
" since echo
is an internal command to cmd.exe
, but it works in the more natural way. Windows batch scripts always surprise me somehow (but not usually in a good way).
旁注:我认为echo
在 " for /f
" 命令的反引号内使用 " "需要使用 " cmd.exe /c echo Test
"来完成,因为它echo
是 的内部命令cmd.exe
,但它以更自然的方式工作。Windows 批处理脚本总是以某种方式让我感到惊讶(但通常不是很好)。
回答by Weeble
Read the documentation for the "for" command: for /?
阅读“for”命令的文档: for /?
Sadly I'm not logged in to Windows to check it myself, but I think something like this can approximate what you want:
遗憾的是我没有登录到 Windows 来自己检查,但我认为这样的事情可以近似于你想要的:
for /F %i in ('echo Test') do my_command %i
回答by mtalexan
Maybe I'm screwing up the syntax of the standard for /f
method, but when I put a very complex command involving && and | within the backticks in the limit of the for /f
, it causes problems. A slight modification from the usual is possible to handle an arbitrary complexity command:
也许我搞砸了标准for /f
方法的语法,但是当我放置一个非常复杂的命令时,涉及 && 和 | 在 限制的反引号内for /f
,它会导致问题。对通常的稍作修改就可以处理任意复杂的命令:
SET VV=some_command -many -arguments && another_command -requiring -the-other -command | handling_of_output | more_handling
for /f "usebackq tokens=*" %%a in (`%VV%`) do mycommand %%a
By putting your full and complex command in a variable first, then putting a reference to the variable in the limit rather than putting the complex command directly into the limit of the for loop, you can avoid syntax interpretation issues. Currently if I copy the exact command I have set to the VV
variable in the example above into where it's used, %VV%
, it causes syntax errors.
通过首先将完整和复杂的命令放入变量中,然后将对该变量的引用放入限制中,而不是将复杂命令直接放入 for 循环的限制中,这样可以避免语法解释问题。目前,如果我将上VV
例中为变量设置的确切命令复制到使用它的位置%VV%
,则会导致语法错误。