backquote and variables

R

robertlaferla

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

Joel VanderWerf

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

Wilson Bilkovich

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
 
J

Joel VanderWerf

Joel said:
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}`
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top