[英]In Windows shell scripting (cmd.exe) how do you assign the stdout of a program to an environment variable?
In UNIX you can assign the output of a script to an environment variable using the technique explained here - but what is the Windows equivalent? 在UNIX中,您可以使用此处说明的技术将脚本的输出分配给环境变量 - 但Windows等效的是什么?
I have a python utility which is intended to correct an environment variable. 我有一个python实用程序,旨在纠正环境变量。 This script simply writes a sequence of chars to stdout.
该脚本只是将一系列字符写入stdout。 For the purposes of this question, the fact that my utility is written in python is irrelevant, it's just a program that I can call from the command-prompt which outputs a single line of text.
出于这个问题的目的,我的实用程序是用python编写的这个事实是无关紧要的,它只是一个程序,我可以从命令提示符调用它输出一行文本。
I'd like to do something like this (that works): 我想做这样的事情(有效):
set WORKSPACE=[ the output of my_util.py ]
After running this command the value of the WORKSPACE environment variable should contain the exact same text that my utility would normally print out. 运行此命令后,WORKSPACE环境变量的值应包含我的实用程序通常打印出来的完全相同的文本。
Can it be done? 可以吗? How?
怎么样?
UPDATE1: Somebody at work suggested: 更新1:有人在工作建议:
python util.py | set /P WORKSPACE=
In theory, that would assign the stdout of the python-script top the env-var WORKSPACE, and yet it does not work, what is going wrong here? 理论上,这会将python脚本的stdout分配给env-var WORKSPACE,但它不起作用,这里出了什么问题?
Use: 采用:
for /f "delims=" %A in ('<insert command here>') do @set <variable name>=%A
For example: 例如:
for /f "delims=" %A in ('time /t') do @set my_env_var=%A
...will run the command "time /t" and set the env variable "my_env_var" to the result. ...将运行命令“time / t”并将env变量“my_env_var”设置为结果。
Remember to use %%A instead of %A if you're running this inside a .BAT file. 如果你在.BAT文件中运行它,请记住使用%% A而不是%A.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.