Forum: Ruby Need tutoring on using a path environment variable

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C0f4cef363d69500730aa9afa9cf7774?d=identicon&s=25 dkmd_nielsen (Guest)
on 2008-11-12 21:56
(Received via mailing list)
I don't know what to do.  I have an environment variable, PW_PATH,
that contains the folder names where applications can be executed
from.  I want to start a particular application that is in one of
those folders.  I currently use the following:

      system("presort /a /nos /watch #{template[0]}")  # submit the
presort to run

This works when the contents of PW_PATH are added to the PATH
variable.  But I would like to avoid that if I can.  I guess my
questions are:  How do I first acquire the contents of PW_PATH?  How
do employ the contents as a temporary addition to PATH so that system
can submit the application and it will be found and executed?

Suggestions?  Comments?  Observations?  All are appreciated.

dvn
D7908f05c89e965f6bc5308ad6f41256?d=identicon&s=25 Siep Korteling (steenslag)
on 2008-11-12 22:08
dkmd_nielsen wrote:
> I don't know what to do.  I have an environment variable, PW_PATH,
> that contains the folder names where applications can be executed
> from.  I want to start a particular application that is in one of
> those folders.  I currently use the following:
>
>       system("presort /a /nos /watch #{template[0]}")  # submit the
> presort to run
>
> This works when the contents of PW_PATH are added to the PATH
> variable.  But I would like to avoid that if I can.  I guess my
> questions are:  How do I first acquire the contents of PW_PATH?  How
> do employ the contents as a temporary addition to PATH so that system
> can submit the application and it will be found and executed?
>
> Suggestions?  Comments?  Observations?  All are appreciated.
>
> dvn

ENV contains all environment variables; ENV['PW_PATH'] is the content of
PW_PATH. So this might be sufficient:

system("#{ENV['PW_PATH']/presort /a /nos /watch #{template[0]}")

hth,

Siep
C0f4cef363d69500730aa9afa9cf7774?d=identicon&s=25 dkmd_nielsen (Guest)
on 2008-11-12 22:15
(Received via mailing list)
On Nov 12, 2:50 pm, dkmd_nielsen <d...@cmscms.com> wrote:
> questions are:  How do I first acquire the contents of PW_PATH?  How
> do employ the contents as a temporary addition to PATH so that system
> can submit the application and it will be found and executed?
>
> Suggestions?  Comments?  Observations?  All are appreciated.
>
> dvn

First question answered:  pw = ENV['pw_path']  I got that.  But I
don't know how to employ the contents such that when the application
is submitted via system that the o/s actually knows where to go
looking.
C0f4cef363d69500730aa9afa9cf7774?d=identicon&s=25 dkmd_nielsen (Guest)
on 2008-11-12 22:20
(Received via mailing list)
On Nov 12, 3:06 pm, Siep Korteling <s.kortel...@gmail.com> wrote:
> > variable.  But I would like to avoid that if I can.  I guess my
>
> system("#{ENV['PW_PATH']/presort /a /nos /watch #{template[0]}")
>
> hth,
>
> Siep
>
> --
> Posted viahttp://www.ruby-forum.com/.

Thanks, Siep.  I will give this a shot.  Not sure if linux and windows/
dos will treat it the same, but I guess that's my job to figure out.
C0f4cef363d69500730aa9afa9cf7774?d=identicon&s=25 dkmd_nielsen (Guest)
on 2008-11-12 22:25
(Received via mailing list)
Let's provide more information.  The contents of pw_path include this:

C:\PW\firstprep;C:\PW\rapid\docs;C:\PW\la;C:\PW\rapid;c:\pw\pst;

And the application I want to execute is presort.exe, which is located
in c:\pw\pst;

I would like to be able to tell the O/S you have to look in the above
folders to find presort.exe, just as you would using PATH.  In a
sense, I would like to temporarily merge the contents of pw_path into
the search path of PATH.

Is that better?
dvn
79caabff3b239b1bee6de8c51bc7f78c?d=identicon&s=25 Rolando Abarca (Guest)
on 2008-11-13 01:24
(Received via mailing list)
On Nov 12, 2008, at 6:22 PM, dkmd_nielsen wrote:

> the search path of PATH.
>
> Is that better?

you can always store the path before and reset it after the system call:

old_path = ENV['path']
ENV['path'] << ENV['PW_PATH']
system(...)
ENV['path'] = old_path

> dvn


regards,
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-11-13 02:33
(Received via mailing list)
On Nov 12, 2008, at 7:20 PM, Rolando Abarca wrote:
>> I would like to be able to tell the O/S you have to look in the above
> ENV['path'] << ENV['PW_PATH']
> system(...)
> ENV['path'] = old_path
>
>> dvn
>
> regards,
> --
> Rolando Abarca M.


Of course, doing that kind of thing in Ruby should look like:

begin
   old_path = ENV['path']
   ENV['path'] << ENV['PW_PATH']
   system(...)
ensure
   ENV['path'] = old_path
end

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
79caabff3b239b1bee6de8c51bc7f78c?d=identicon&s=25 Rolando Abarca (Guest)
on 2008-11-13 15:20
(Received via mailing list)
On Nov 12, 2008, at 10:30 PM, Rob Biedenharn wrote:

> Of course, doing that kind of thing in Ruby should look like:
>
> begin
>  old_path = ENV['path']
>  ENV['path'] << ENV['PW_PATH']
>  system(...)
> ensure
>  ENV['path'] = old_path
> end

totally right... although, I don't think that system will raise an
exception. Maybe if PATH is blank (i.e. nil), concatenating the other
environmental variable will raise an exception.

> -Rob
>
> Rob Biedenharn    http://agileconsultingllc.com
> Rob@AgileConsultingLLC.com


regards,
B1b1d33e0655e841d4fd8467359c58d0?d=identicon&s=25 Yossef Mendelssohn (Guest)
on 2008-11-13 15:46
(Received via mailing list)
On Nov 12, 6:20 pm, Rolando Abarca <funkas...@gmail.com> wrote:
> you can always store the path before and reset it after the system call:
>
> old_path = ENV['path']
> ENV['path'] << ENV['PW_PATH']
> system(...)
> ENV['path'] = old_path

First,

>> str = 'blah'
=> "blah"
>> old_str = str
=> "blah"
>> str << 'test'
=> "blahtest"
>> str
=> "blahtest"
>> old_str
=> "blahtest"

Maybe you mean ENV['path'] += ENV['PW_PATH'], or maybe you mean
old_path = ENV['path'].dup

Second, paths have separators, so simply sticking two different path
strings together is going to make something strange at the join.

Third, I think you mean ENV['PATH'], not ENV['path'].


HTH HAND
79caabff3b239b1bee6de8c51bc7f78c?d=identicon&s=25 Rolando Abarca (Guest)
on 2008-11-13 16:13
(Received via mailing list)
On Nov 13, 2008, at 11:42 AM, Yossef Mendelssohn wrote:

> Maybe you mean ENV['path'] += ENV['PW_PATH'], or maybe you mean
> old_path = ENV['path'].dup

oops... you're right (I always forget about this! I even wrote a
question about this for the ruby-basic java black belt[1]
certification test :-P)

> Second, paths have separators, so simply sticking two different path
> strings together is going to make something strange at the join.

yes sir, I skip those on purpose... :-) (it was too late and it was
the last email I wrote before going to bed. I think that the OP should
know that PATH is a list of directories that are separated by
something, or at least he has the notion, so I only provided an idea
on how to solve his problem, not a complete solution, which, IMHO is
the best thing, because when you provide a complete solution, most
people stick to that and don't think too much around it).
Anyway, you should add the path like this:

ENV['PATH'] += ";#{ENV['PW_PATH']}" # just in case ENV['PW_PATH'] is nil

even if PATH has a ";" at the end, it does no harm to have another one.

> Third, I think you mean ENV['PATH'], not ENV['path'].

That's also correct.

> HTH HAND
> --
> -yossef


[1]
http://www.javablackbelt.com/QuestionnaireDefDispl...

regards,
This topic is locked and can not be replied to.