使用 Windows 批处理文件中的命令结果设置变量的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/889518/
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
Set the value of a variable with the result of a command in a Windows batch file
提问by Francis
When working in a Bashenvironment, 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
是命令设置的变量command -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
被设置为command -args
而不是命令的输出。
回答by nik
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 Cygwinon your Windows system if you are used to Unix-type scripting.
但是,如果您习惯了 Unix 类型的脚本,我建议您在 Windows 系统上使用Cygwin。
回答by Jonathan Headland
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 批处理时未能清除变量的值可能会导致细微错误。
回答by lotsoffreetime
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”中第一行(仅)的值。
回答by Jesse Dearing
回答by alchi baucha
Set "dateTime="
For /F %%A In ('powershell get-date -format "{yyyyMMdd_HHmm}"') Do Set "dateTime=%%A"
echo %dateTime%
pause
Official Microsoft docsfor
for
command
微软官方文档的
for
命令