ENV['LD_LIBRARY_PATH'] setting in Ruby don't take effect in csh env

Aassume the value of $LD_LIBRARY_PATH is "/usr/dt/lib:/usr/local/lib".

In ruby, I do setting below.

ENV['LD_LIBRARY_PATH']='/home/test/LDPATH:/usr/dt/lib:/usr/local/lib',

then I want to check it with csh 'env' command as below.

%x[ /bin/csh -c 'env' ].each_line do |_line|
                        puts _line
end

But still get LD_LIBRARY_PATH=/usr/dt/lib:/usr/local/lib.

Seems the 'LD_LIBRARY_PATH' setting in ruby don't influence the output
of csh 'env' command, Why? And how can we make the setting take effect
in c shell env also?

Thanks,
Previn

···

--
Posted via http://www.ruby-forum.com/.

Quoting Previn Lin (lists@ruby-forum.com):

Seems the 'LD_LIBRARY_PATH' setting in ruby don't influence the output
of csh 'env' command, Why? And how can we make the setting take effect
in c shell env also?

I am not familiar with csh, but it has to be that you have some
configuration file which is read when csh is invoked (with bash you
have .bashrc). If one of those files sets that variable, it overrides
your setting from within Ruby.

To see which files are opened when csh starts, you may try with

strace -eopen /bin/csh

Carlo

···

Subject: ENV['LD_LIBRARY_PATH'] setting in Ruby don't take effect in csh env
  Date: mar 03 dic 13 06:57:58 +0100

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

Carlo E. Prelz wrote in post #1129313:

Subject: ENV['LD_LIBRARY_PATH'] setting in Ruby don't take effect in csh
env
  Date: mar 03 dic 13 06:57:58 +0100

Quoting Previn Lin (lists@ruby-forum.com):

Seems the 'LD_LIBRARY_PATH' setting in ruby don't influence the output
of csh 'env' command, Why? And how can we make the setting take effect
in c shell env also?

I am not familiar with csh, but it has to be that you have some
configuration file which is read when csh is invoked (with bash you
have .bashrc). If one of those files sets that variable, it overrides
your setting from within Ruby.

To see which files are opened when csh starts, you may try with

strace -eopen /bin/csh

Carlo

Dear Carlo,

Should be not that way.

It can be easy verified by steps below.

1. set in .cshrc
setenv LD_LIBRARY_PATH /usr/dt/lib:/usr/local/lib

2. cat test.rb
#!/usr/bin/env ruby

ENV['LD_LIBRARY_PATH'] = '/home/test/LDPATH:/usr/dt/lib:/usr/local/lib'

%x[ /bin/csh -c 'env' ].each_line do |_line|
        puts _line
end

Then run test.rb can check the LD_LIBRARY_PATH, it will be:

LD_LIBRARY_PATH=/usr/dt/lib:/usr/local/lib

Not LD_LIBRARY_PATH=/home/test/LDPATH:/usr/dt/lib:/usr/local/lib we want
it to be.

Thanks,
Previn

···

--
Posted via http://www.ruby-forum.com/\.

Carlo E. Prelz wrote in post #1129313:

Subject: ENV['LD_LIBRARY_PATH'] setting in Ruby don't take effect in csh
env
  Date: mar 03 dic 13 06:57:58 +0100

Quoting Previn Lin (lists@ruby-forum.com):

Seems the 'LD_LIBRARY_PATH' setting in ruby don't influence the output
of csh 'env' command, Why? And how can we make the setting take effect
in c shell env also?

I am not familiar with csh, but it has to be that you have some
configuration file which is read when csh is invoked (with bash you
have .bashrc). If one of those files sets that variable, it overrides
your setting from within Ruby.

To see which files are opened when csh starts, you may try with

strace -eopen /bin/csh

Carlo

Dear Carlo,

Is there way can avoid this then?

Thanks,
Previn

···

--
Posted via http://www.ruby-forum.com/\.

Quoting Previn Lin (lists@ruby-forum.com):

Is there way can avoid this then?

I took the time to download csh to have a look... Apparently, if you
execute csh as

/bin/csh -f

it won't process .cshrc. Of course, this means that none of the
commands that are included there will be executed.

Note that the flag only disables processing of the .cshrc file in your
home directory. There is a system-wide conf file (/etc/csh.cshrc)
which, apparently, cannot be overridden, so that if the variable is
set there, there is no way to avoid the setting.

Keep in mind that, once you have executed csh, the underlying Ruby
process has no control anymore on its environment (it is a separate
process). Thus, if you want to control csh's idea of LD_LIBRARY_PATH
from Ruby (or from any other calling process), make sure that it is
not clobbered by csh init files. You may modify the csh init file,
say, by setting the variable only if it is not previously set.

(you do understand that playing with LD_LIBRARY_PATH opens a can of
security-related worms, eh?)

Carlo

···

Subject: Re: ENVsetting in Ruby don't take effect in csh env
  Date: mar 03 dic 13 08:10:09 +0100

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

I took the time to download csh to have a look... Apparently, if you
execute csh as

/bin/csh -f

it won't process .cshrc. Of course, this means that none of the
commands that are included there will be executed.

Too bad,why add "-f" option without influence? Why I still get the
content for the virable of LD_LIBRARY_PATH that defined in .cshrc? I run
with "csh -f test.csh".

215 %cat test.csh
#!/bin/csh -f

echo $LD_LIBRARY_PATH

Keep in mind that, once you have executed csh, the underlying Ruby
process has no control anymore on its environment (it is a separate
process). Thus, if you want to control csh's idea of LD_LIBRARY_PATH
from Ruby (or from any other calling process), make sure that it is
not clobbered by csh init files. You may modify the csh init file,
say, by setting the variable only if it is not previously set.

Suppose "csh -f" works, source csh file in ruby still won't get the
virables defined in ruby by ENV['LD_LIBRARY_PATH]', am I right? Seems I
need more study on this issue. :slight_smile:

Thanks for your time and warm help.

···

--
Posted via http://www.ruby-forum.com/\.

(you do understand that playing with LD_LIBRARY_PATH opens a can of
security-related worms, eh?)

Dear Carlo,

We only use the script internally, so this should be no problems.

I just want setup a shared ruby script for team members, we want the
LD_LIBRARY_PATH set in ruby not lost when ruby script source another
tool setup related csh file.

···

================================
Under a csh terminal, if you

setenv LD_LIBRARY_PATH /home/abc

and source a test.csh file similiar as below

cat test.csh
setenv LD_LIBRARY_PATH /usr/openwin/lib:$LD_LIBRARY_PATH

after run test.csh, it skip LD_LIBRARY_LIBRARY setting in .cshrc, and
get $LD_LIBRARY_PATH as: /usr/openwin/lib:/home/abc

=========================================================================
Just want to know whetehr ruby can do same thing? I test it as below,
but seems it read .cshrc first and can't get same results as under csh
terminal

<1> ENV['LD_LIBRARY_PATH'] = '/home/abc'
<2> %x[ /bin/csh -c '#{source test.csh};env' ].each_line do |_line|
             ENV.store *_line.chomp.split(/[=:]/, 2)
             end
     end

--
Posted via http://www.ruby-forum.com/\.

Carlo E. Prelz wrote in post #1129323:

I took the time to download csh to have a look... Apparently, if you
execute csh as

/bin/csh -f

it won't process .cshrc. Of course, this means that none of the
commands that are included there will be executed.

Dear Carlo,

You're right, I just realize that I should do this(/bin/csh -f) in ruby
when source the c shell file, instead of adding it in the head of the "c
shell file" that I did before. "/home_directory/.cshrc" will be skipped
when do as way below in ruby:

              %x[ /bin/csh -f -c '#{source_cmd};env' ].each_line do

_line|

                      if (_line =~ /^\S+=\S+$/ or _line =~
/^\S+:\s*\S+/)
                          ENV.store *_line.chomp.split(/[=:]/, 2)
                      end
              end

···

--
Posted via http://www.ruby-forum.com/\.

Quoting Previn Lin (lists@ruby-forum.com):

================================
Under a csh terminal, if you

setenv LD_LIBRARY_PATH /home/abc

and source a test.csh file similiar as below

cat test.csh
setenv LD_LIBRARY_PATH /usr/openwin/lib:$LD_LIBRARY_PATH

after run test.csh, it skip LD_LIBRARY_LIBRARY setting in .cshrc, and
get $LD_LIBRARY_PATH as: /usr/openwin/lib:/home/abc

=========================================================================
Just want to know whetehr ruby can do same thing? I test it as below,
but seems it read .cshrc first and can't get same results as under csh
terminal

This has nothing to do with ruby. If you run test.csh from csh, it
will not start a new instance of csh. Init scripts are executed when a
new instance of csh is started. Of course, if you run '/bin/csh' from
the Ruby interpreter, you will start a new instance of csh.

Old instance of csh runs Ruby (which knows nothig about csh)
Ruby runs new instance of csh (which knows nothing about Ruby)

You may want to post your question on a csh-related list. There might
be tricks available. As I already mentioned, I know nowthing about csh.

Carlo

···

Subject: Re: ENVsetting in Ruby don't take effect in csh env
  Date: mar 03 dic 13 11:20:10 +0100

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

You may want to post your question on a csh-related list. There might
be tricks available. As I already mentioned, I know nowthing about csh.

Carlo

Thanks a lot, Carlo, you're right, this is most likely shell/subshell
related issue. But I think discuss it here still some meaningful, for
it's common issue we may meet. In fact, it's an extend for post
Is there way to source csh env file in Ruby? - Ruby - Ruby-Forum. Gennady,Robert, Michael
and some other friends gave the solution on source usage in ruby. The
script comes from a real requirement that need setup different version
tools and corresponding env. When I add another tool, I met this
LD_LIBRARY_PATH issue. I set LD_LIBRARY_PATH in ruby, but after source
csh file for the tool, I found the LD_LIBRARY_PATH set lost, it make the
tool launch fail.

Anyway, I use a not good but straight-forward solution like below.
Anyone can give better solution will be very appreciated.

def source_csh_file(csh_file)
        if FileTest::exist?("#{csh_file}")
                source_cmd = "source #{csh_file}"
                %x[ /bin/csh -c '#{source_cmd};env' ].each_line do

_line|

                        if (_line =~ /^\S+=\S+$/ or _line =~
/^\S+:\s*\S+/)
                                ENV.store *_line.chomp.split(/[=:]/, 2)
                        end
                end
        end
end

def process_ld_before_source
        if ENV['LD_LIBRARY_PATH']
                ld_path = ENV['LD_LIBRARY_PATH']
        else
                ld_path = ''
        end
        return ld_path
end

def process_ld_after_source(ld_path)
        ld1_path = ld_path
        ld2_path = process_ld_before_source
        ld_path = (ld2_path + ":" + ld1_path).split(":").reject {|x| not
File.directory?"#{x}"}.uniq.join(":")
        ENV['LD_LIBRARY_PATH'] = ld_path
end

ld_path = process_ld_before_source
csh_file = "#{@homedir}/setup/setup.csh"
source_csh_file(csh_file)
process_ld_after_source(ld_path)

···

--
Posted via http://www.ruby-forum.com/\.