I think the issue is that the “system” method runs a command in a
subshell, and therefore your program’s executing environment doesn’t
actually ever change
Check out this link for more info:
Maybe you could achieve what you are looking for by wrapping it in
another ruby script, ie:
Script 1:
system “source test.csh && ruby script_2.rb”
I think the issue is that the “system” method runs a command in a
subshell, and therefore your program’s executing environment doesn’t
actually ever change
I think the issue is that the “system” method runs a command in a
subshell, and therefore your program’s executing environment doesn’t
actually ever change
Maybe you could achieve what you are looking for by wrapping it in
another ruby script, ie:
Script 1:
system “source test.csh && ruby script_2.rb”
Script2:
puts “a = #{ENV[‘SYSTYPE’]}”
Hi Michael,
With modification as below, it can get right results.
cat script_1.rb
#!/usr/bin/env ruby
system “csh -c ‘source test.csh && script_2.rb’”
But I need process it in one file instead of in “script_2.rb”, in fact,I
need source a cshell file(the test.csh here is just a simple example,
not complete file) to setup env, then start tool accordingly.
So, is there way can source the csh file and still can get the set env
virables in current shell?
Maybe you could achieve what you are looking for by wrapping it in
With modification as below, it can get right results.
So, is there way can source the csh file and still can get the set env
virables in current shell?
%x[ csh -c ‘source test.csh; env’ ].lines.each do |_line|
ENV.store *_line.chomp.split(‘=’, 2)
end
Maybe you could achieve what you are looking for by wrapping it in
With modification as below, it can get right results.
So, is there way can source the csh file and still can get the set env
virables in current shell?
%x[ csh -c ‘source test.csh; env’ ].lines.each do |_line|
ENV.store *_line.chomp.split(‘=’, 2)
end
Thanks, Gennady, get all env virables by run shell command env,process
and store with Ruby ENV commands, it works, many thanks.
For the attached test case, I wrote ruby as below, but seems don’t work.
My bad. This is embarrassing. I am sorry.
I still owe you a proper reply. Here it is:
$ ./test.rb
a = Linux
$ cat test.rb
#!/usr/bin/env ruby
IO.popen([‘csh’, ‘-c’, ‘source test.csh && setenv’]) do |io|
io.each_line do |line|
if %r{^([^=]+)=(.*)$} =~ line
ENV[$1] = $2
else
warn “Cannot read line %p” % line
end
end
end
puts “a = #{ENV[‘SYSTYPE’]}”
The code could be extended in the following ways:
more error checking (e.g. existence of test.csh)
handle variables with values that contain a newline
filter, e.g. include / exclude special variable names or by entries
already present in the current environment.
Sorry for the delay in replying, did not have an opportunity to review
the list communication. Thanks to Michael B., you got an
explanation earlier :-). Rubys * in front of an array is called splat
operator. For one, it is used to perform in-place array expansion for
method parameters. The following irb session may help in understanding
the concept:
irb(main):001:0> def f(p1, p2)
irb(main):002:1> puts “P1: #{p1.inspect}, P2: #{p2.inspect}”
irb(main):003:1> end
=> nil
irb(main):004:0> a = [ 1, 2 ]
=> [1, 2]
irb(main):005:0> f(a)
ArgumentError: wrong number of arguments (1 for 2)
from (irb):5:in f' from (irb):5 from :0 irb(main):006:0> f(*a) P1: 1, P2: 2 => nil irb(main):007:0> a = [ 1, 2, 3 ] => [1, 2, 3] irb(main):008:0> f(*a) ArgumentError: wrong number of arguments (3 for 2) from (irb):8:in f’
from (irb):8
from :0
irb(main):009:0>
Notice that method f(p1, p2) accepts exactly 2 parameters. So invocation
f(a) fails as it gets only one parameter (array a). However f(*a)
succeeds when array a contains exactly 2 parameters, with a[0] becoming
p1 and a[1] - p2. However, when a has 3 elements, f(*a) fails with
diagnostics that 3 parameters are passed while 2 expected.
For the attached test case, I wrote ruby as below, but seems don’t work.
My bad. This is embarrassing. I am sorry.
No problem :-). With so many useful tips and solutions you provide on
this list, you are entitled to an occasional misread ;-). Thanks a lot
for your work.
For the attached test case, I wrote ruby as below, but seems don’t work.
My bad. This is embarrassing. I am sorry.
I still owe you a proper reply. Here it is:
$ ./test.rb
a = Linux
$ cat test.rb
#!/usr/bin/env ruby
IO.popen([‘csh’, ‘-c’, ‘source test.csh && setenv’]) do |io|
io.each_line do |line|
if %r{^([^=]+)=(.*)$} =~ line
ENV[$1] = $2
else
warn “Cannot read line %p” % line
end
end
end
puts “a = #{ENV[‘SYSTYPE’]}”
The code could be extended in the following ways:
more error checking (e.g. existence of test.csh)
handle variables with values that contain a newline
filter, e.g. include / exclude special variable names or by entries
already present in the current environment.
The last one is easy, just change one line
ENV[$1] ||= $2
Kind regards
robert
Dear Robert,
Thanks for so detailed guide, many thanks.
By the way, is there something mis-writing? When I run the test.rb you
gave, it reports error below, I’m still checking why?
test.rb:3:in `popen’: can’t convert Array into String (TypeError)