在Windows shell脚本(cmd.exe)中,如何将程序的stdout分配给环境变量? [英] In Windows shell scripting (cmd.exe) how do you assign the stdout of a program to an environment variable?

查看:299
本文介绍了在Windows shell脚本(cmd.exe)中,如何将程序的stdout分配给环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在UNIX中,您可以使用以下技术将脚本的输出分配给环境变量:这里 - 但是什么是Windows等同的?

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?

我有一个python实用程序,更正环境变量。这个脚本只是将一个字符序列写入stdout。为了这个问题的目的,我的实用程序是写在python是无关紧要的,它只是一个程序,我可以从命令提示符输出一行文本。

I have a python utility which is intended to correct an environment variable. This script simply writes a sequence of chars to 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.

我想这样做(工作):

set WORKSPACE=[ the output of my_util.py ]

运行此命令后,WORKSPACE环境变量的值应包含完全相同的文本我的实用程序通常打印出来。

After running this command the value of the WORKSPACE environment variable should contain the exact same text that my utility would normally print out.

可以做吗?如何?

UPDATE1:有人在工作时建议:

UPDATE1: Somebody at work suggested:

python util.py | set /P WORKSPACE=

理论上,这将分配python脚本的stdout顶部env -var 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?

推荐答案

使用:

for /f "delims=" %A in ('<insert command here>') do @set <variable name>=%A

例如:

for /f "delims=" %A in ('time /t') do @set my_env_var=%A

...将运行命令time / t并将env变量my_env_var设置为结果。

...will run the command "time /t" and set the env variable "my_env_var" to the result.

%% A而不是%A,如果你在.BAT文件中运行这个。

Remember to use %%A instead of %A if you're running this inside a .BAT file.

这篇关于在Windows shell脚本(cmd.exe)中,如何将程序的stdout分配给环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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