Page 1 of 1 1
Topic Options
#206639 - 2013-02-06 10:34 AM Problem with environment vars
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 307
Loc: STRASBOURG, France
Hi,

first, sorry for my english \:\)
i recently discovered a problem with environment var.
Look at the following code
 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
 Code:
$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 :
 Code:
;-- 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 :
 Code:
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
 Code:
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
#206662 - 2013-02-09 06:14 AM Re: Problem with environment vars [Re: ChristopheM]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11610
Loc: CA
Is this different than your other post on this similar subject?
Top
#206677 - 2013-02-11 03:25 PM Re: Problem with environment vars [Re: NTDOC]
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 307
Loc: STRASBOURG, France
this post was my original post
but i have reformulated the problem so that i can be a suggestion
_________________________
Christophe

Top
#206687 - 2013-02-15 09:08 AM Re: Problem with environment vars [Re: ChristopheM]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4517
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
 Code:
@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
 Code:
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
 Code:
[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
 Code:
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
#206688 - 2013-02-15 09:21 AM Re: Problem with environment vars [Re: Allen]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4517
Loc: USA
In the bat file you could also set %exe% to run kix32 and your script if you wanted. The new path should go over the newly opened kix32 with no problems.
Top
#206689 - 2013-02-15 01:49 PM Re: Problem with environment vars [Re: Allen]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4517
Loc: USA
You could also replace the readprofilestring code with the following UDF.

 Code:

$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
#206690 - 2013-02-15 01:50 PM Re: Problem with environment vars [Re: Allen]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22344
Loc: OK
I just can't see how this is useful... but, it surely works.
_________________________
!

download KiXnet

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 292 anonymous users online.
Newest Members
Praveer, Ollero, rayyo, Foxlicht, DonChild
17742 Registered Users

Generated in 0.033 seconds in which 0.012 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org