使用 Windows 批处理文件中的命令结果设置变量的值 [英] Set the value of a variable with the result of a command in a Windows batch file

查看:37
本文介绍了使用 Windows 批处理文件中的命令结果设置变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bash 环境中工作时,要将变量的值设置为命令的结果,我通常会这样做:

When working in a Bash environment, to set the value of a variable as the result of a command, I usually do:

var=$(command -args)

其中 var 是由命令 command -args 设置的变量.然后我可以以 $var 的形式访问该变量.

where var is the variable set by the command command -args. I can then access that variable as $var.

与几乎所有 Unix shell 兼容的更传统的方法是:

A more conventional way to do this which is compatible with almost every Unix shell is:

set var=`command -args`

也就是说,如何使用 Windows 批处理文件中的命令结果设置变量的值?我试过了:

That said, how can I set the value of a variable with the result of a command in a Windows batch file? I've tried:

set var=command -args

但我发现 var 设置为 command -args 而不是命令的输出.

But I find that var is set to command -args rather than the output of the command.

推荐答案

从 Windows 批处理文件中执行 Jesse 描述的操作你需要写:

To do what Jesse describes, from a Windows batch file you will need to write:

for /f "delims=" %%a in ('ver') do @set foobar=%%a 

但是,如果您习惯了 Unix 类型的脚本,我建议您在 Windows 系统上使用 Cygwin.

But, I instead suggest using Cygwin on your Windows system if you are used to Unix-type scripting.

这篇关于使用 Windows 批处理文件中的命令结果设置变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆