[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

setting environment variables using windows batch file

Afaque Ahmad

5/26/2005 12:16:00 PM

Dear Ruby Members,

I am having problems setting and reading enviroment variables from a windows batch file.

This is what I am doing, I have some batch file xyz.bat where I am setting some enviroment
variable

SET ENV_VAR1 = "something"
SET ENV_VAR2 = "foo"

I wish to run the batch file from ruby script , wherein which I can use these environment variables within the ruby file itself. I use the following command to run the batch file

system("xyz.bat")

But even after doing this , the environment variables ENV_VAR1 and ENV_VAR2 don't get set.
I even tried using
exec("xyz.bat")
system("cmd /k xyz.bat")

I would really appreciate it if anybody could tell me what I am doing wrong here.

Regards,
Afaque






---------------------------------
Yahoo! Mail Mobile
Take Yahoo! Mail with you! Check email on your mobile phone.
2 Answers

Austin Ziegler

5/26/2005 12:28:00 PM

0

On 5/26/05, Afaque Ahmad <ahmads_786@yahoo.com> wrote:
> I am having problems setting and reading enviroment variables from
> a windows batch file.

Simply put, you can't do this. #exec and #system start new copies of
the command interpreter (cmd.exe) and the environment variables are
set in that environment, which is subordinate to the Ruby
interpreter process.

You'd be better off parsing the BAT file for SET commands. And using ENV.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Brian Schröder

5/26/2005 12:36:00 PM

0

On 26/05/05, Afaque Ahmad <ahmads_786@yahoo.com> wrote:
> Dear Ruby Members,
>
> I am having problems setting and reading enviroment variables from a windows batch file.
>
> This is what I am doing, I have some batch file xyz.bat where I am setting some enviroment
> variable
>
> SET ENV_VAR1 = "something"
> SET ENV_VAR2 = "foo"
>
> I wish to run the batch file from ruby script , wherein which I can use these environment variables within the ruby file itself. I use the following command to run the batch file
>
> system("xyz.bat")
>
> But even after doing this , the environment variables ENV_VAR1 and ENV_VAR2 don't get set.
> I even tried using
> exec("xyz.bat")
> system("cmd /k xyz.bat")
>
> I would really appreciate it if anybody could tell me what I am doing wrong here.
>

Hello Afaque,

it should be possible if you can invert your problem. If you start the
ruby interpreter from the batch file, you have the variables set.

--8<---test.bat---8<---
SET FOO = "foo"
ruby test.rb
--8<-----------------8<--


--8<---test.rb---8<---
puts ENV['FOO']
--8<-----------------8<--

If you want to use the batch file as some kind of configuration file,
maybe you could revert to a real configuration file. E.g. in yaml.

best regards,

Brian Schröder

--
http://ruby.brian-sch...

Stringed instrument chords: http://chordlist.brian-sch...