[英]How to set an env variable with multiline file content in windows command prompt
I have a file say my-file.dat
which has multiple lines.我有一个文件说my-file.dat
有多行。 I want to set an env variable say MYVAR
that will contain the content of my-file.dat.我想设置一个环境变量,比如MYVAR
,它将包含 my-file.dat 的内容。 That is, in windows command prompt, the output of type my-file.dat
be same as the output of echo %MYVAR%
and with new-line preserved .也就是说,在 windows 命令提示符下, type my-file.dat
的 output 与echo %MYVAR%
的 output保留相同,并带有新的。
In RHEL/MAC, it is typically done like this export MYVAR=$(cat my-file.dat)
, but how do I do the similar in windows command prompt (not really interested in powershell, but feel free to share examples it might be my backup option)在 RHEL/MAC 中,它通常像这样export MYVAR=$(cat my-file.dat)
完成,但是我如何在 windows 命令提示符下执行类似操作(对 powershell 并不真正感兴趣,但随时分享它可能的示例成为我的备份选项)
Following the answer of this Stackoverflow question Multiline text file, how to put into an environment variable I could get a variable to hold the content of my-file.dat.按照这个 Stackoverflow 问题Multiline text file, how to put into an environment variable我可以得到一个变量来保存 my-file.dat 的内容。 But this variable's scope seems to be the batch file.但是这个变量的 scope 似乎是批处理文件。 Even though I run the batch file from the command prompt, I don't see the batch-file's variable is available in the command prompt.即使我从命令提示符运行批处理文件,我也没有在命令提示符中看到批处理文件的变量。
I tried set /P MYVAR=<my-file.dat
, but this sets only the first line whereas I want all lines when I echo MYVAR我尝试set /P MYVAR=<my-file.dat
,但这仅设置第一行,而当我回显 MYVAR 时我想要所有行
Pls help.请帮忙。
The technique you use requires delayed expansion, which is not enabled by default.您使用的技术需要延迟扩展,默认情况下不启用。 So the code issues setlocal enableDelayedExpansion
.所以代码发出setlocal enableDelayedExpansion
。 But that localizes environment changes.但这会本地化环境变化。 When the script ends there is an implicit endlocal
, and the environment is restored to what existed before the setlocal
.当脚本结束时有一个隐含的endlocal
,并且环境恢复到setlocal
之前的状态。
The simplest solution is to run the script in a session where delayed expansion is already enabled so that you can remove setlocal enableDelayedExpansion
.最简单的解决方案是在已启用延迟扩展的 session 中运行脚本,以便您可以删除setlocal enableDelayedExpansion
。 You can do this simply by running你可以简单地通过运行来做到这一点cmd /v:on
from your command line before executing your script. cmd /v:on
在执行脚本之前从命令行中打开。
There is another simple, but ugly option.还有另一个简单但丑陋的选择。 The implicit endlocal does not occur if your script has a fatal syntax error (I consider this to be a bug in cmd.exe).如果您的脚本有致命的语法错误(我认为这是 cmd.exe 中的错误),则不会发生隐式 endlocal。 So you can put the following at the end of your script:因此,您可以将以下内容放在脚本的末尾:
:: Your current script goes here.
:: I'm assuming your script falls through to the end and does not use EXIT /B
call :fatalErr 2>nul
:fatalErr
if
But I discourage you from using this technique if you might be executing your script multiple times - your dead environments will begin to pile up.但是,如果您可能多次执行脚本,我不鼓励您使用这种技术 - 您的死环境将开始堆积。
Also - please remember that variables are limited to ~8191 characters - your script will fail if the file you are trying to capture exceeds the limit.另外 - 请记住,变量限制为 ~8191 个字符 - 如果您尝试捕获的文件超出限制,您的脚本将失败。 This is a hard limit of cmd.exe这是 cmd.exe 的硬限制
Update更新
You could put the cmd /v:on
command within your script if you add the /K option.如果添加 /K 选项,则可以将cmd /v:on
命令放在脚本中。 The IF statement tests if delayed expansion is enabled or not. IF 语句测试是否启用延迟扩展。 If not, then it reruns the script via cmd.exe with the /K option and /V:ON.如果不是,则它通过 cmd.exe 使用 /K 选项和 /V:ON 重新运行脚本。
@echo off
if "!!" neq "" cmd /v:on /k "%~f0"
:: rest of your script goes here.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.