[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

backquote and variables

Robert La ferla

10/24/2006 6:15:00 PM

I am trying to construct a command line to be executed using backquote `` notation. i.e. capturing the output to a string

e.g.

#!/usr/bin/ruby -w
file = "/etc/motd"
string = `"cat " + file`
puts string

% ruby test.rb
sh: line 1: cat : command not found

This doesn't work. It appears that everything between the backquotes is treated as a quoted string. i.e. no variable substitution takes place.

How can I work around this?

3 Answers

Joel VanderWerf

10/24/2006 6:24:00 PM

0

robertlaferla@comcast.net wrote:
> I am trying to construct a command line to be executed using backquote `` notation. i.e. capturing the output to a string
>
> e.g.
>
> #!/usr/bin/ruby -w
> file = "/etc/motd"
> string = `"cat " + file`


string = `#{"cat "} + file`


> puts string
>
> % ruby test.rb
> sh: line 1: cat : command not found
>
> This doesn't work. It appears that everything between the backquotes is treated as a quoted string. i.e. no variable substitution takes place.
>
> How can I work around this?


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Wilson Bilkovich

10/24/2006 6:28:00 PM

0

On 10/24/06, robertlaferla@comcast.net <robertlaferla@comcast.net> wrote:
> I am trying to construct a command line to be executed using backquote `` notation. i.e. capturing the output to a string
>
> e.g.
>
> #!/usr/bin/ruby -w
> file = "/etc/motd"
> string = `"cat " + file`
> puts string
>
> % ruby test.rb
> sh: line 1: cat : command not found
>
> This doesn't work. It appears that everything between the backquotes is treated as a quoted string. i.e. no variable substitution takes place.
>
> How can I work around this?
>
>

The backtick string is treated like a double-quote. Therefore, you can do this:
file = '/etc/motd'
motd = `cat #{file}`
puts motd

Joel VanderWerf

10/24/2006 6:36:00 PM

0

Joel VanderWerf wrote:
> robertlaferla@comcast.net wrote:
>> I am trying to construct a command line to be executed using backquote
>> `` notation. i.e. capturing the output to a string
>>
>> e.g.
>>
>> #!/usr/bin/ruby -w
>> file = "/etc/motd"
>> string = `"cat " + file`
>
>
> string = `#{"cat "} + file`

Oops, I didn't really read your example. I assumed you were trying to
get the command name interpolated in the string. Wilson answered your
question.

If you wanted to interpolate both, you could do this:

cmd="cat"
file="/etc/motd"
motd = `#{cmd} #{file}`

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407