How to "source" a file?

I have a bash file ~/.myenv that contains environmental variables like:

export EDITOR=vi
export FOO=bar
...

How do I source this file from within a ruby script that starts like?

#!/usr/bin/ruby

I could source the file in bash before running the ruby script, but I
prefer that the script do it, in case the user forgets.

Thx.

-Ken

···

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

Ken Feng wrote:

I have a bash file ~/.myenv that contains environmental variables like:

export EDITOR=vi
export FOO=bar
...

How do I source this file from within a ruby script that starts like?

#!/usr/bin/ruby

I could source the file in bash before running the ruby script, but I
prefer that the script do it, in case the user forgets.

Thx.

-Ken

`source ~/.myenv`

at work.. NOT TESTED.. guess

ilan

···

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

Since Ruby is not Bash, you have to parse the file:

    File.foreach("myenv") do |line|
      line.chomp.scan(/^export (.+?)=(.*)$/) do |name, val|
        ENV[name] = val
      end
    end

    p ENV['FOO'] # => "bar"

···

At 2009-08-07 10:48AM, "Ken Feng" wrote:

I have a bash file ~/.myenv that contains environmental variables like:

export EDITOR=vi
export FOO=bar
..

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous

Discount Ed hardy tshirt (www.ebuyings.com)
Discount Ed hardy jean (www.ebuyings.com)
Discount Ed hardy shoes (www.ebuyings.com)
Discount Ed hardy handbag (www.ebuyings.com)
Discount Ed hardy other porduct (www.ebuyings.com)
Discount Nike air jordans (www.ebuyings.com)
Discount Nike Air Max 90 Sneakers (www.ebuyings.com)
Discount Nike Air Max 91 Supplier (www.ebuyings.com)
Discount Nike Air Max 95 Shoes Supplier (www.ebuyings.com)
Discount Nike Air Max 97 Trainers (www.ebuyings.com)
Discount Nike Air Max 2003 Wholesale (www.ebuyings.com)
Discount Nike Air Max 2004 Shoes Wholesale
(www.ebuyings.com)
Discount Nike Air Max 2005 Shop (www.ebuyings.com)
Discount Nike Air Max 2006 Shoes Shop (www.ebuyings.com)
Discount Nike Air Max 360 Catalogs (www.ebuyings.com)
Discount Nike Air Max Ltd Shoes Catalogs (www.ebuyings.com)
Discount Nike Air Max Tn Men's Shoes (www.ebuyings.com)
Discount Nike Air Max Tn 2 Women's Shoes (www.ebuyings.com)
Discount Nike Air Max Tn 3 Customize (www.ebuyings.com)
Discount Nike Air Max Tn 4 Shoes Customize
( www.ebuyings.com)
Discount Nike Air Max Tn 6 Supply (www.ebuyings.com)
Discount Nike Shox NZ Shoes Supply (www.ebuyings.com)
Discount Nike Shox OZ Sale (www.ebuyings.com)
Discount Nike Shox TL Store (www.ebuyings.com)
Discount Nike Shox TL 2 Shoes Store (www.ebuyings.com)
Discount Nike Shox TL 3 Distributor (www.ebuyings.com)
Discount Nike Shox Bmw Shoes Distributor (www.ebuyings.com)
Discount Nike Shox Elite Shoes Manufacturer
(www.ebuyings.com)
Discount Nike Shox Monster Manufacturer (www.ebuyings.com)
Discount Nike Shox R4 Running Shoes (www.ebuyings.com)
Discount Nike Shox R5 Mens Shoes (www.ebuyings.com)
Discount Nike Shox Ride Womens Shoes (www.ebuyings.com)
Discount Nike Shox Rival Shoes Wholesaler (www.ebuyings.com)
Discount Nike Shox Energia Wholesaler (www.ebuyings.com)
Discount Nike Shox LV Sneaker (www.ebuyings.com)
Discount Nike Shox Turbo Suppliers (www.ebuyings.com)
Discount Nike Shox Classic Shoes Suppliers
(www.ebuyings.com)
Discount Nike Shox Dendara Trainer (www.ebuyings.com)
Discount Nike Air Jordan 1 Seller (www.ebuyings.com)
Discount Nike Air Jordan 2 Shoes Seller (www.ebuyings.com)
Discount Nike Air Jordan 3 Collection (www.ebuyings.com)
Discount Nike Air Jordan 4 Shoes Collection
(www.ebuyings.com)
Discount Nike Air Jordan 5 Chaussure Shoes
(www.ebuyings.com)
Discount Nike Air Jordan 6 Catalog (www.ebuyings.com)
Discount Nike Air Jordan 7 Shoes Catalog (www.ebuyings.com)
Discount Nike Air Jordan 8 Customized (www.ebuyings.com)
Discount Nike Air Jordan 9 Shoes Customized
(www.ebuyings.com)
Discount Nike Air Jordan 10 Wholesalers (www.ebuyings.com)
Discount Nike Jordan 11 Shoes Wholesalers (www.ebuyings.com)
Discount Nike Air Jordan 12 Factory (www.ebuyings.com)
Discount Nike Air Jordan 13 Shoes Factory (www.ebuyings.com)
Discount Nike Air Jordan 14 Shoes Sell (www.ebuyings.com)
Discount Nike Air Jordan 16 Exporter (www.ebuyings.com)
Discount Nike Air Jordan 17 Shoes Exporter
(www.ebuyings.com)
Discount Nike Air Jordan 18 Offer (www.ebuyings.com)
Discount Nike Air Jordan 19 Shoes Offer (www.ebuyings.com)
Discount Nike Air Jordan 20 Manufacture (www.ebuyings.com)
Discount Nike Jordan 21 Shoes Manufacture (www.ebuyings.com)

Nope! That will set those variables in the child process and any of its children, but they'll go away and have no effect on your ruby process.

Your best bet is to warn the user if you are using defaults and tell him/her/yourself to fix it.

unless ENV['EDITOR']
   ENV['EDITOR'] = 'vi'
   puts "Environment has no EDITOR using '#{ENV['EDITOR']}'"
   puts "Did you mean to?: source ~/.myenv"
end

You could also exit without doing anything if the value has no acceptable default.

-Rob

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

···

On Aug 7, 2009, at 11:00 AM, Ilan Berci wrote:

Ken Feng wrote:

I have a bash file ~/.myenv that contains environmental variables like:

export EDITOR=vi
export FOO=bar
...

How do I source this file from within a ruby script that starts like?

#!/usr/bin/ruby

I could source the file in bash before running the ruby script, but I
prefer that the script do it, in case the user forgets.

Thx.

-Ken

`source ~/.myenv`

at work.. NOT TESTED.. guess

ilan

That's pretty fragile -- it would severely limit what you can put in the file.

There are a few ways of doing this, depending on your goal. One would be to
run a bash (or better yet, sh) script as a wrapper around your Ruby script --
that is, if your command is installed as /usr/local/bin/foo, have it be a
shell script which does this:

#!/bin/sh
. ~/.myenv
exec /usr/lib/foo/foo.rb $@

Another hack I've used, when I wasn't sure whether or not I'd need those
variables, is to have bash execute the file, and then tell me. If you only need
one variable, this is easy:

ENV['FOO'] = `source ~/.myenv && echo -n $FOO`

If you wanted all of the variables, here's one (clumsy) way to do it without
having to (really) parse anything:

require 'yaml'
s = `source ~/.myenv && ruby -e 'require "yaml"; print ENV.to_hash.to_yaml`
Yaml.load(s).each_pair{|k,v| ENV[k]=v}

I realize I could've made that less ugly, but I'm not sure there's a good way
to make it pretty. It's essentially the same as extracting a single variable,
except that it invokes another Ruby interpreter to dump the entire environment
as a yaml stream. I did this because it's a lot easier to just throw the yaml
parser at the problem than try to parse the output of something like 'env' --
I couldn't even figure out which environment variables existed, otherwise.

About the only way I could "improve" it is by streaming that Yaml
generation/parsing, but if anyone has an environment big enough for it to
matter, they've already got problems.

One final way would be to add a 'source ~/.myfile' to any other programs you try
to run -- since you mentioned EDITOR, I'm assuming that's why.

···

On Friday 07 August 2009 02:55:11 pm Glenn Jackman wrote:

Since Ruby is not Bash, you have to parse the file:

    File.foreach("myenv") do |line|
      line.chomp.scan(/^export (.+?)=(.*)$/) do |name, val|
        ENV[name] = val
      end
    end

    p ENV['FOO'] # => "bar"

I don't understand this comment. How is it "severely limiting"?

Granted, it won't properly parse
    export a=b c=d e=f foo bar
and I didn't account for extra whitespace. The OP didn't specify any
such requirements though.

But it's not going to get tripped up by any line that doesn't match the
pattern. It won't run off and execute any of the other commands in this
bash file.

I'm not personally offended by your comments, I'm curious to learn more.

···

At 2009-08-07 06:53PM, "David Masover" wrote:

On Friday 07 August 2009 02:55:11 pm Glenn Jackman wrote:
> Since Ruby is not Bash, you have to parse the file:
>
> File.foreach("myenv") do |line|
> line.chomp.scan(/^export (.+?)=(.*)$/) do |name, val|
> ENV[name] = val
> end
> end
>
> p ENV['FOO'] # => "bar"

That's pretty fragile -- it would severely limit what you can put in
the file.

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous

A couple of lines from my .profile:

export PATH=${HOME}/bin:${PATH}
source ${HOME}/.bashrc
if [ -z "$COLORTERM" ]
then
        export EDITOR=vi
fi

/Kent

···

Den 10 Aug 2009 16:57:42 GMT skrev Glenn Jackman:

At 2009-08-07 06:53PM, "David Masover" wrote:

On Friday 07 August 2009 02:55:11 pm Glenn Jackman wrote:
> Since Ruby is not Bash, you have to parse the file:
>
> File.foreach("myenv") do |line|
> line.chomp.scan(/^export (.+?)=(.*)$/) do |name, val|
> ENV[name] = val
> end
> end
>
> p ENV['FOO'] # => "bar"

That's pretty fragile -- it would severely limit what you can put in
the file.

I don't understand this comment. How is it "severely limiting"?

Granted, it won't properly parse
    export a=b c=d e=f foo bar
and I didn't account for extra whitespace. The OP didn't specify any
such requirements though.

But it's not going to get tripped up by any line that doesn't match the
pattern. It won't run off and execute any of the other commands in this
bash file.

I'm not personally offended by your comments, I'm curious to learn more.

--
"The Brothers are History"