如何批量变量设置为一个脚本的输出 [英] How to set batch variable to output of another script
问题描述
我尝试一个批次变量设置为另一个的命令的输出。在的Linux / Unix,你可以简单地使用反引号,例如(在csh)
I try to set a batch variable to an output of another command. In Linux/Unix you can simply use backticks, e.g. (in csh)
set MY_VAR = `tail /etc/passwd`
有没有类似的东西可以在Windows批处理?
Is there something similar available in windows batch?
其实我已经发现的东西,但它不完全工作:
Actually I found already something but it is not fully working:
d:\>for /F "skip=1" %n in ('wmic OS Get CurrentTimeZone') do set TimeZone=%n
d:\>set TimeZone=120
:\>set TimeZone=
d:\>
问题是 WMIC
命令返回几行,否则会正常工作。第一个我所知道的跳跃,但是我没能跳过第二个空行。我与试图IF
,但没有成功。
The problem is the wmic
commands returns several lines, otherwise it would work fine. The first I know to skip, however I did not manage to skip the second empty line. I tried with IF
but no success.
推荐答案
是 - WMIC的输出是一个有点难看处理
yes - the output of wmic is a bit ugly to handle.
使用一招:寻找在输出中的数字( FINDSTR[0-9]
将只返回行,包含数字):
Use a trick: search for a number in the ouput (findstr "[0-9]
will only return lines, that contain a number):
for /F %n in ('wmic OS Get CurrentTimeZone ^|findstr "[0-9]"') do set TimeZone=%n
echo Timezone is %TimeZone%.
(用于在使用批处理文件使用 %% n和
而不是%N
)
另一种方法是:
for /F %n in ('wmic OS Get CurrentTimeZone') do if not defined TimeZone set TimeZone=%n
编辑:
我preFER的第一个版本,为 FINDSTR
(或找到
)转换WMIC设备 - 结局,因此通过 MC ND 提到的第二个为
时并不需要
I prefer the first version, as findstr
(or find
) converts the wmic-line-endings, so the second for
mentioned by MC ND is not neccessary.
这篇关于如何批量变量设置为一个脚本的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!