backquotes question

Z

Zouplaz

Hello, I would like to know why
cmd = "/bin/df -H /dev/#{partition}"
used = `#{cmd}`

works fine but this fails
used = `"/bin/df -H /dev/#{partition}"`

with an OS error.

Thank you

BTW, is there an aliase to the backquote ? I mean a kernel method doing
the same - Just for the syntax, backquotes are not very sexy.
 
T

Tim Hunter

Zouplaz said:
Hello, I would like to know why
cmd = "/bin/df -H /dev/#{partition}"
used = `#{cmd}`

works fine but this fails
used = `"/bin/df -H /dev/#{partition}"`

with an OS error.

Thank you

BTW, is there an aliase to the backquote ? I mean a kernel method doing
the same - Just for the syntax, backquotes are not very sexy.

You don't need the quotes when you surround the command with backquotes.
Use this instead:

used = `/bin/df -H /dev/#{partition}`

When you use both quotes and backquotes, the quotes are treated as part
of the command. That's what causes the error.

You can use %x instead of backquotes:

used = %x(/bin/df -H /dev/#{partition})
 
7

7stud --

Zouplaz said:
Hello, I would like to know why
cmd = "/bin/df -H /dev/#{partition}"
used = `#{cmd}`

works fine but this fails
used = `"/bin/df -H /dev/#{partition}"`

with an OS error.

Well, first of all they aren't equivalent. For instance, writing this:

str = "hello"
puts "Hi, #{str} world."

isn't equivalent to writing:

puts "Hi, "hello" world."

I don't know enough about Unix to know the reason for the system error,
but this works:

flags = "-l"
puts `ls #{flags}`

But, this does not:

puts `"ls -l"`

The internal quotes messes things up.

BTW, is there an aliase to the backquote ? I mean a kernel method doing
the same - Just for the syntax, backquotes are not very sexy.

puts %x{ls -l}
 
B

Brian Adkins

Hello, I would like to know why
cmd = "/bin/df -H /dev/#{partition}"
used = `#{cmd}`

works fine but this fails
used = `"/bin/df -H /dev/#{partition}"`

with an OS error.

Thank you

BTW, is there an aliase to the backquote ? I mean a kernel method doing
the same - Just for the syntax, backquotes are not very sexy.

I think the syntax is great, but you can also use the following:

puts Kernel::`("ls")

Still uses a backquote, but it's more like a traditional method call.

Or as described on p. 351 of the pickaxe, you can alias the backquote:

alias :cmd :`
puts cmd "ls"

This worked for me when running a program, but not from irb.

But if you give it some time, I think you may like the simplicity of:

result = `ls`
 
G

Gerardo Santana Gómez Garrido

2007/10/13 said:
Hello, I would like to know why
cmd = "/bin/df -H /dev/#{partition}"
used = `#{cmd}`

works fine but this fails
used = `"/bin/df -H /dev/#{partition}"`

with an OS error.

Let partition be "wd0a".

In the first case, you're executing:

/bin/df -H /dev/wd0a

which the shell interprets correctly as a call the program /bin/df
with the specified options and parameters.

In the second case you're executing:

"/bin/df -H /dev/wd0a"

and the shell cannot find the program named "/bin/df -H /dev/wd0a"
 
Z

Zouplaz

le 14/10/2007 05:21, Gerardo Santana Gómez Garrido nous a dit:
Let partition be "wd0a".

In the first case, you're executing:

/bin/df -H /dev/wd0a

which the shell interprets correctly as a call the program /bin/df
with the specified options and parameters.

In the second case you're executing:

"/bin/df -H /dev/wd0a"

and the shell cannot find the program named "/bin/df -H /dev/wd0a"

Thanks for the hint (and others posters too) !
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top