使用Windows批处理文件中的命令结果设置变量的值

时间:2023-01-29 23:30:35

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

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

var=$(command -args)

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

其中var是命令命令-args设置的变量。然后我可以将该变量作为$ var访问。

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

执行此操作的更常规方法几乎与每个Unix shell兼容:

set var=`command -args`

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:

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

set var=command -args

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

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

4 个解决方案

#1


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

要执行Jesse所描述的内容,您需要从Windows批处理文件中编写:

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

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

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

#2


One needs to be somewhat careful, since the Windows batch command:

需要注意一点,因为Windows批处理命令:

for /f "delims=" %%a in ('command') do @set theValue=%%a

does not have the same semantics as the Unix shell statement:

与Unix shell语句没有相同的语义:

theValue=`command`

Consider the case where the command fails, causing an error.

考虑命令失败的情况,导致错误。

In the Unix shell version, the assignment to "theValue" still occurs, any previous value being replaced with an empty value.

在Unix shell版本中,仍然会发生对“theValue”的赋值,任何以前的值都将替换为空值。

In the Windows batch version, it's the "for" command which handles the error, and the "do" clause is never reached -- so any previous value of "theValue" will be retained.

在Windows批处理版本中,它是处理错误的“for”命令,并且永远不会达到“do”子句 - 因此将保留“theValue”的任何先前值。

To get more Unix-like semantics in Windows batch script, you must ensure that assignment takes place:

要在Windows批处理脚本中获得更多类似Unix的语义,必须确保进行分配:

set theValue=
for /f "delims=" %%a in ('command') do @set theValue=%%a

Failing to clear the variable's value when converting a Unix script to Windows batch can be a cause of subtle errors.

将Unix脚本转换为Windows批处理时未能清除变量的值可能是导致细微错误的原因。

#3


Here's how I do it when I need a database query's results in my batch file:

当我在批处理文件中需要数据库查询结果时,我就是这样做的:

sqlplus -S schema/schema@db @query.sql> __query.tmp
set /p result=<__query.tmp
del __query.tmp

The key is in line 2: "set /p" sets the value of "result" to the value of the first line (only) in "__query.tmp" via the "<" redirection operator.

键位于第2行:“set / p”通过“<”重定向运算符将“result”的值设置为“__query.tmp”中第一行(仅)的值。

#4


The only way I've seen it done is if you do this:

我看过它的唯一方法就是你这样做:

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

ver is the version command for Windows and on my system it produces:

ver是Windows的版本命令,在我的系统上它生成:

Microsoft Windows [Version 6.0.6001]

Source

#1


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

要执行Jesse所描述的内容,您需要从Windows批处理文件中编写:

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

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

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

#2


One needs to be somewhat careful, since the Windows batch command:

需要注意一点,因为Windows批处理命令:

for /f "delims=" %%a in ('command') do @set theValue=%%a

does not have the same semantics as the Unix shell statement:

与Unix shell语句没有相同的语义:

theValue=`command`

Consider the case where the command fails, causing an error.

考虑命令失败的情况,导致错误。

In the Unix shell version, the assignment to "theValue" still occurs, any previous value being replaced with an empty value.

在Unix shell版本中,仍然会发生对“theValue”的赋值,任何以前的值都将替换为空值。

In the Windows batch version, it's the "for" command which handles the error, and the "do" clause is never reached -- so any previous value of "theValue" will be retained.

在Windows批处理版本中,它是处理错误的“for”命令,并且永远不会达到“do”子句 - 因此将保留“theValue”的任何先前值。

To get more Unix-like semantics in Windows batch script, you must ensure that assignment takes place:

要在Windows批处理脚本中获得更多类似Unix的语义,必须确保进行分配:

set theValue=
for /f "delims=" %%a in ('command') do @set theValue=%%a

Failing to clear the variable's value when converting a Unix script to Windows batch can be a cause of subtle errors.

将Unix脚本转换为Windows批处理时未能清除变量的值可能是导致细微错误的原因。

#3


Here's how I do it when I need a database query's results in my batch file:

当我在批处理文件中需要数据库查询结果时,我就是这样做的:

sqlplus -S schema/schema@db @query.sql> __query.tmp
set /p result=<__query.tmp
del __query.tmp

The key is in line 2: "set /p" sets the value of "result" to the value of the first line (only) in "__query.tmp" via the "<" redirection operator.

键位于第2行:“set / p”通过“<”重定向运算符将“result”的值设置为“__query.tmp”中第一行(仅)的值。

#4


The only way I've seen it done is if you do this:

我看过它的唯一方法就是你这样做:

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

ver is the version command for Windows and on my system it produces:

ver是Windows的版本命令,在我的系统上它生成:

Microsoft Windows [Version 6.0.6001]

Source