[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Dany Cayouette

5/12/2005 8:46:00 PM

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

3 Answers

Guillaume Marcais

5/12/2005 10:40:00 PM

0

On Fri, 2005-05-13 at 06:00 +0900, Dany Cayouette wrote:
> 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.




Dany Cayouette

5/13/2005 1:59:00 AM

0

Guillaume Marcais wrote:

>
> 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


gga

5/13/2005 4:04:00 PM

0

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.