Nuby: 'importing shell script variables' eval/proc/binding

D

Dany Cayouette

Greetings,
I'm pretty sure eval/proc/binding needs/can be used to solve my
problem, but I'm having difficulty grasping those concepts.

Given a series of shell scripts (bourne based) containing code such as

FIRST=Dany
LAST='Cayouette'
FULLNAME="$FIRST $LAST"
UID="danyc ($FULLNAME)"

I would like to 'import' those value into a Ruby Hash such that the end
result is something like
{"LAST"=>"Cayouette", "UID"=>"danyc (Dany Cayouette)", "FULLNAME"=>"Dany
Cayouette", "FIRST"=>"Dany"}

So far I have
class Hash
def shparse (file)
File.open(file, "r") do |f|
while line = f.gets
next unless line =~ /^\s*(\w+)=(.*)$/
k = $1
v = $2
self[k] = v
end
end
p self
self
end
end


Which gives me some of the basics, but not the 'expansion' of $VARS.

Any help/input appreciated.
Dany
 
G

Guillaume Marcais

Greetings,
I'm pretty sure eval/proc/binding needs/can be used to solve my
problem, but I'm having difficulty grasping those concepts.

Given a series of shell scripts (bourne based) containing code such as

FIRST=Dany
LAST='Cayouette'
FULLNAME="$FIRST $LAST"
UID="danyc ($FULLNAME)"

I would like to 'import' those value into a Ruby Hash such that the end
result is something like
{"LAST"=>"Cayouette", "UID"=>"danyc (Dany Cayouette)", "FULLNAME"=>"Dany
Cayouette", "FIRST"=>"Dany"}

So far I have
class Hash
def shparse (file)
File.open(file, "r") do |f|
while line = f.gets
next unless line =~ /^\s*(\w+)=(.*)$/
k = $1
v = $2
self[k] = v
end
end
p self
self
end
end

Two solutions to you problem. The first one shell_expand actually calls
the shell. So the expansion should be exactly what it is supposed to be.
But it is probably fragile: on my system, the variable UID is already
defined and read-only, and it would break shell_expand.

The other one tries to mimic the shell variable expansion in ruby. Of
course, not all the shell expansions are performed (as seen with
SHELL_ONLY).

$ cat shell-expand.rb
#!/usr/bin/ruby -w

script = <<EOT
FIRST=Dany
LAST='Cayouette'
FULLNAME="$FIRST $LAST"
UUID="danyc ($FULLNAME)"
EMPTY=

junk foo
SHELL_ONLY=$(date)
EOT

def shell_expand(script)
res = { }
io = open("|/bin/sh", "r+")
script.each { |l|
next unless l =~ /^(\w+)\s*=/
io.puts(l.chomp)
io.puts("echo $#{$1}")
res[$1] = (io.gets || "").chomp
}
res
end

def self_expand(script)
res = { }
script.each { |l|
next unless l =~ /^(\w+)\s*=\s*['"]?(.*?)['"]?$/
k, v = $1.strip, $2.strip
v = v.gsub(/\$(\w+)/) { |m| res[$1] || "" }
res[k.strip] = v
}
res
end

p shell_expand(script)
p self_expand(script)


$ ruby shell-expand.rb
{"EMPTY"=>"", "LAST"=>"Cayouette", "FULLNAME"=>"Dany Cayouette",
"SHELL_ONLY"=>"Thu May 12 18:33:56 EDT 2005", "UUID"=>"danyc (Dany
Cayouette)", "FIRST"=>"Dany"}
{"EMPTY"=>"", "LAST"=>"Cayouette", "FULLNAME"=>"Dany Cayouette",
"SHELL_ONLY"=>"$(date)", "UUID"=>"danyc (Dany Cayouette)",
"FIRST"=>"Dany"}

Hope it helps,
Guillaume.
 
D

Dany Cayouette

Guillaume said:
Two solutions to you problem. The first one shell_expand actually calls
the shell. So the expansion should be exactly what it is supposed to be.
But it is probably fragile: on my system, the variable UID is already
defined and read-only, and it would break shell_expand.
....
The other one tries to mimic the shell variable expansion in ruby. Of


Hope it helps,
Guillaume.
wow... I was really going down the wrong path. The 'script' example
given was 'bogus'. UID was probably just a wrong example to use. For
my simple case, both solution given should work. Thanks a lot for the help.
Dany
 
G

gga

The easiest and most correct solution is to
a) run the appropiate shell for your script, If using #! notation,
running the script itself should do the trick.
b) rely on the command called "env" with no parameters to list all the
variables as key -> value pairs from within your shell script, after
the script is run.
c) capture that output of b) to a pipe or file that your ruby script
would then read in and parse out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top