#206639 - 2013-02-06 10:34 AM
Problem with environment vars
|
ChristopheM
Hey THIS is FUN
   
Registered: 2002-05-13
Posts: 309
Loc: STRASBOURG, France
|
Hi,
first, sorry for my english i recently discovered a problem with environment var. Look at the following code
$path = "c:\rep1;c:\;"+%path%
SETL "PATH="+$path
SHELL 'cmd /c start "rep1" path'
$path = "c:\rep2;c:\;"+%path%
SETL "PATH="+$path
SHELL 'cmd /c start "rep2" path' In the first console, path is c:\rep1;c:\;... In the second console, path is c:\rep2;c:\;... There is no reference to c:\rep1.
According to kix help, this is normal because setl doesn't change environment var for current process. I suppose SETL use an internal array to save modifications and this array is used before call to SHELL or RUN to create the new environment block for sub-process.
In the script, a solution could be
$path = "c:\rep1;c:\;"+%path%
SETL "PATH="+$path
SHELL 'cmd /c start "rep1" path'
$path = "c:\rep2;"+$path
SETL "PATH="+$path
SHELL 'cmd /c start "rep2" path'
Now, real scripts are not as simples. We have scripts to launch applications. theses scripts use generic functions to configure global modules like oracle client or java and specific functions to configure the application to launch. It seems very difficult to use global variables :
;-- configure generic modules --
$=ConfigureOracle11()
$=ConfigureJava16()
;-- configure application --
set path="d:\appl\winapp32\my appli dir;"+%path%
SHELL "d:\appl\winapp32\my appli dir\my appli.exe;"
exit
function ConfigureOracle11()
SETL PATH="c:\program files\oracle11\bin;"+%path%
endfunction
function ConfigureJava16()
SETL PATH="c:\program files\java\jre\bin;"+%path%
endfunction
In this case, we lost reference to oracle 11 and java path in the resultant path. Has somebody already seen this problem and found an elegant solution ?
I think an elegant solution could be a modification of ExpandEnvironmentVars function to add an optional parameter to get modifications made with setl or a new function GetL. The sample script would become :
SETL "PATH="+ExpandEnvironmentVars( "c:\rep1;c:\;"+%path%, 1 )
SHELL 'cmd /c start "rep1" path'
SETL "PATH="+ExpandEnvironmentVars( "c:\rep2;"+%path%, 1 )
SHELL 'cmd /c start "rep2" path' or
SETL "PATH="+"c:\rep1;c:\;"+GetL( "PATH" )
SHELL 'cmd /c start "rep1" path'
SETL "PATH="+"c:\rep2;"+GetL( "PATH" )
SHELL 'cmd /c start "rep2" path'
A suggestion for ruud ???
PS : i have the same problem of environment vars with a function i wrote to launch program with WMI. I have no acces to vars modified with setl and process launched via WMI do not see modified vars !!!
_________________________
Christophe
|
Top
|
|
|
|
#206687 - 2013-02-15 09:08 AM
Re: Problem with environment vars
[Re: ChristopheM]
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4557
Loc: USA
|
Here is how I think I would attempt to solve this problem.
First a little background. I knew I had done something like this in the past with for /f in the batch file. It took me forever to remember how to do it, and to figure why it was failing. Finally found the following thread that helped my compromised mind... http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=130273#Post130273
So here is my setup...
path.bat
@echo off
set apps=Oracle11;Java16
set exe=yourexename
for /f "tokens=*" %%f in ('kix32 path.kix') do set path=%%f
echo %path%
::%exe%
pause
path.kix
break on
$RC=setoption("WrapatEOL","ON")
$RC=setoption("ASCII","on")
;? "%apps%"
$apps=split("%apps%",";")
for each $app in $apps
if $newpath
$newpath=$newpath + ";"
endif
$newpath=$newpath + readprofilestring(@scriptdir + "\paths.ini",$App,"Path")
next
"%path%;" + $newpath
paths.ini
[Oracle11]
Path="c:\program files\oracle11\bin"
[Java16]
Path="c:\program files\java\jre\bin"
Path.bat Notes 1. Set the Apps you need separated by ; (These app names correspond to sections in the INI) 2. Set the EXE Name 3. Runs Kix32 path.kix and whatever is displayed to the screen from the path.kix script is set to the path var 4. Displays the path 5. Runs the exe (remove :: first)
Once you have all your code in the path.kix correct, the only thing you will have to do is modify the first two vars of path.bat for each of your apps.
Path.kix Notes 1. ASCII ON so that whatever is displayed in the console is accessible by the for /f loop 2. Looks up each Section in the ini file based on the %apps% var from the bat. Each section contains the path for the particular app 3. Modify the code so that you manipulate the path anyway you want. 4. The last thing to do is display the value you want the path to be
While testing you might consider doing something like this
for /f "tokens=*" %%f in ('kix32 path.kix') do echo %%f
Last thing, I was working on this in wee of the night... couldn't sleep until i figured it out. Hope it is helpful, but won't be the least bit surprised to find bugs in my code.
|
Top
|
|
|
|
#206689 - 2013-02-15 01:49 PM
Re: Problem with environment vars
[Re: Allen]
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4557
Loc: USA
|
You could also replace the readprofilestring code with the following UDF.
$newpath=$newpath + GetAppPath($app)
function GetAppPath($AppName)
select
case $AppName="Java16"
$GetAppPath="c:\program files\java\jre\bin"
case $AppName="Oracle11"
$GetAppPath="c:\program files\oracle11\bin"
endselect
endfunction
Originally, I was thinking of doing more with the INI, but never did.
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1739 anonymous users online.
|
|
|