将环境变量传递给Subshel​​l CMD [英] Pass environment variables to subshell CMD

查看:90
本文介绍了将环境变量传递给Subshel​​l CMD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows上的gitlab-ci.yml中有问题.我使用环境变量启动phpunit.
所以,我有一个像这样的变量:

I have a problem in my gitlab-ci.yml on Windows. I launch phpunit with environments variables.
So, I have a variable like:

PHPUNIT : %SOURCE_PATH%\cgi-bin\php.exe %PHPUNIT_PATH%

并且在前面声明了一些变量:

And some variables are declared before:

SOURCE_PATH: 'C:\Source'
PHPUNIT_PATH: '"%SOURCE_PATH%\cgi-bin\tests\__init\tools\phpunit.phar"'

但是当我使用 CALL 命令时,Windows不会在另一个变量中解析该变量.

But when I use the CALL command, Windows doesn't resolve the variable inside the other variable.

所以,如果我这样做:

CALL Echo %PHPUNIT%

我有:

C:\Source\cgi-bin\php.exe "%SOURCE_PATH%\cgi-bin\tests\__init\tools\phpunit.phar"

我认为这是因为%SOURCE_PATH% CALL 上下文中不存在.

I think this is because of %SOURCE_PATH% doesn't exist in CALL context.

但是我找不到如何在 CALL 中传递环境变量的方法.而且我也找不到其他方法来做到这一点. (如果我不使用 CALL ,当内部脚本退出时, gitlab-ci 将停止.)

But I could not find out how to pass the environment variables in the CALL. And I could not find another way to do this, too. (If I don't use CALL, the gitlab-ci stops when the inside script exits.)

希望您能帮助我...

I wish you can help me ...

要知道的东西.

我的脚本是由 gitlab-ci 运行程序启动的,因此它是由以下程序启动的:

My script is launched by gitlab-ci runner, so it is started by:

setlocal enableextensions
setlocal enableDelayedExpansion
set nl=^

而且我无法更改.

如果需要,或者您知道其他解决方法,我可以使用PowerShell. :)

I can use PowerShell if needed, or if you know another work around. :)

推荐答案

此批处理文件在执行时演示了该问题并提供了解决方案.

This batch file demonstrates on execution the problem and offers a solution.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
cls
set "SOURCE_PATH=C:\Source"
set "PHPUNIT_PATH="%%SOURCE_PATH%%\cgi-bin\tests\__init\tools\phpunit.phar""
echo %SOURCE_PATH%\cgi-bin\php.exe %PHPUNIT_PATH%
echo/
echo Reference to environment variable SOURCE_PATH in environment
echo variable PHPUNIT_PATH is not expanded on running php.exe.
echo/
echo Solution:
echo/
echo Explicitly set environment variable PHPUNIT_PATH once more with
echo its own value with using additionally the command CALL to expand
echo all variable references inside the variable value.
echo/
call set "PHPUNIT_PATH=%PHPUNIT_PATH%"
echo %SOURCE_PATH%\cgi-bin\php.exe %PHPUNIT_PATH%
echo/
endlocal
pause

不要多次运行 SETLOCAL 来通过指定参数EnableExtensionsEnableDelayedExpansion来定义命令行环境.只能在运行 SETLOCAL 时指定两次.请参阅此答案以获取有关命令 SETLOCAL ENDLOCAL 的详细解释. >这样做应该明确为什么不建议频繁运行 SETLOCAL 的原因.

Don't run SETLOCAL more than once for defining the command line environment with specifying the parameters EnableExtensions and EnableDelayedExpansion. Both can be specified on running SETLOCAL only once. See this answer for a detailed explanation what the commands SETLOCAL and ENDLOCAL do which should make it clear why it is not advisable to run SETLOCAL more often than really needed.

仅当确实需要将延迟扩展作为处理文本或目录/文件名或参数字符串中的感叹号时才启用延迟扩展,而不再将其作为启用延迟扩展的文字字符来处理.

And enable delayed expansion only when really needed as exclamation marks in text to process or directory/file names or parameter strings are handled not anymore as literal character with delayed expansion enabled.

这篇关于将环境变量传递给Subshel​​l CMD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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