简体   繁体   English

在Windows shell脚本(cmd.exe)中,如何将程序的标准输出分配给环境变量?

[英]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.

相关问题 如何在子进程弹出的情况下清除“cmd.exe”的 STDOUT? - How to clear the STDOUT of 'cmd.exe' with subprocess popen? 如何为 Windows 上的 cmd.exe shell 安全地转义命令行 arguments? - How to securely escape command line arguments for the cmd.exe shell on Windows? 在Windows的cmd.exe Shell中,如何将Perl脚本的输出传递给Python脚本? - In Windows' cmd.exe shell, how can I pipe the output of a Perl script to a Python script? CMD.exe如何识别扩展需要程序? - How CMD.exe recognize an extension need program? 我的程序在Python shell中运行,但在cmd.exe中运行时无法识别我的输入 - My program works in Python shell but doesn't recognize my input when it runs in cmd.exe Python - Stray cmd.exe窗口导致程序挂起。 我如何关闭这个迷路程序? - Python - Stray cmd.exe window causes program to hang. How can I close this stray program? 如何在Python中使用参数执行cmd.exe - How to execute cmd.exe with arguments in Python Scrapy在Python Shell和cmd.exe中有不同的结果 - Scrapy has different results in Python shell and cmd.exe Python 未安装且终端 shell 路径“cmd.exe”不存在 - Python is not installed and The terminal shell path “cmd.exe” does not exist 在程序完成之前,cmd.exe不会重定向错误 - cmd.exe does not redirect errors until the program is finished
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM