将批处理命令输出到变量 [英] Outputting Batch Command to Variable
问题描述
我在批处理脚本方面没有很强的背景,因此,如果有明显的答案,我们深表歉意.
I do not have a strong background in batch scripts, so apologies if there is an obvious answer.
我正在尝试将命令提示符的输出定向到变量.我可以将其输出到这样的文件
I am trying to direct the output of a command prompt to a variable. I can output it to a file like this
go test -race > output.txt
在这种情况下,将使用go test -race的输出填充output.txt.我想做的就是将其输出到一个变量,类似
In this case, output.txt gets populated with the output of go test -race. What I am trying to do is output it to a variable, something along the lines of
set iamavariable=nothotdog
go test -race > %iamavariable%
echo %location%
这将创建一个名为nothotdog的文件,该文件具有go test -race的输出,并且回显nothotdog,而不是回显go test -race的输出
This creates a file called nothotdog that has the output of go test -race, and echoes nothotdog, instead of echoing the output of go test -race
推荐答案
要正确获取命令结果,您需要一个for
循环.
To correctly get the result of a command, you need a for
loop.
for /f %%p in ('your command') do set result=%%p
echo %result%
如果命令输出的不只是单个单词/数字/字符/数字,那么可能会更好:
If the command was outputting more than a single word/number/character/digit then perhaps this would be better:
For /F "Delims=" %%A In ('go test -race') Do Set "result=%%A"
Echo %result%
这篇关于将批处理命令输出到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!