Command Line Interface Ruby

Go To StackoverFlow.com

2

Say I have the code:

print(">>> ")
command = gets

What I want to do, is execute the command variable as one would normally do in Ruby, by putting backticks around the code you want to execute. How would I do this? If I do:

`command`

it just tries to execute the word command. How do I execute the variable???

2012-04-03 20:05
by Billjk
Be very careful doing this. Allowing a user to enter a command, then executing it for them, allows them to execute the command with the permissions your code runs under. If it runs as root, and the user enters "rm -rf /", your system will go bye-bye really fast - the Tin Man 2012-04-03 21:55
yep i know! I'm obviously not gonna release this to other people, I just got the idea from the book Metaprogramming Ruby. About a page after, it told me how bad of an idea it was hah - Billjk 2012-04-04 01:33


5

Just interpolate it like you would with a string:

`#{command}`
2012-04-03 20:13
by Andrew Marshall
+1 You can guess things like this in Ruby and surprisingly often it just works - steenslag 2012-04-03 20:19
Ads