Pass environment variables to subshell CMD

一个人想着一个人 提交于 2020-01-05 06:48:40

问题


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"'

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

So if I do:

CALL Echo %PHPUNIT%

I have:

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

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

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 ...

Somethings to know.

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

setlocal enableextensions
setlocal enableDelayedExpansion
set nl=^

And I can't change this.

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


回答1:


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

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.




回答2:


Your Question is Not so much clear... But, I may guess that - Your inbetween Scripts are using 'Exit' at the end. SO, Replace it with Exit /B

Or Try to Use 'Start' Instead of Call... (Start /B ... - Check 'Start /?')



来源:https://stackoverflow.com/questions/42602064/pass-environment-variables-to-subshell-cmd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!