From: Guillaume Marcais Date: 2005-05-13T07:40:02+09:00 Subject: Re: Nuby: 'importing shell script variables' eval/proc/binding 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 = <"", "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.