10

I need to execute a Bash command in a Ruby script. There are about 6 ways to do this according to "6 Ways to Run Shell Commands in Ruby" by Nate Murray and a few other googled sources.

print "enter myid: "
myID = gets
myID = myID.downcase
myID = myID.chomp 
print "enter host: "
host = gets
host = host.downcase
host = host.chomp 
print "winexe to host: ",host,"\n"
command = "winexe -U domain\\\\",ID," //",host," \"cmd\""
exec command 
1
  • What class do you expect command to be? Commented Nov 16, 2011 at 21:36

2 Answers 2

8

For what it's worth you can actually chain those methods, and puts will print a newline for you, so this could just be:

print "enter myid: "
myID = STDIN.gets.downcase.chomp

print "enter host: "
host = STDIN.gets.downcase.chomp

puts "winexe to host: #{host}"
command = "winexe -U dmn1\\\\#{myID} //#{host} \"cmd\""
exec command
Sign up to request clarification or add additional context in comments.

3 Comments

perfect thank you. It was my first ruby script ever so thank you also for the note about chaining those methods, its somthing ill use a lot i think.
You can also use the string concatenation operator (+) if you don't want to interpolate the variables inside a quoted string: command = "winexe -U dmn1\\\\" + myId + " //" + host + ' "cmd"', or use Ruby's printf-like syntax: command = 'winexe -U dmn1\\\\%s //%s "cmd"' % [myId, host]
I would actually use Ruby's alternate quote syntax for this, but I didn't want to introduce too many changes at once. This would work: command = %Q{winexe -U dmn1\\\\#{myID} //#{host} "cmd"}. Here's some more info: en.wikibooks.org/wiki/Ruby_Programming/Alternate_quotes
6

It looks like there may have been trouble with how you were putting your command string together.
Also, I had to refer to STDIN directly.

# Minimal changes to get it working:
print "enter myid: "

myID = STDIN.gets
myID = myID.downcase
myID = myID.chomp

print "enter host: "
host = STDIN.gets
host = host.downcase
host = host.chomp 

print "winexe to host: ",host,"\n"
command = "echo winexe -U dmn1\\\\#{myID} //#{host} \"cmd\""
exec command



Compact version:

print "enter myid: "
myID = STDIN.gets.downcase.chomp

print "enter host: "
host = STDIN.gets.downcase.chomp

puts "winexe to host: #{host}"
exec "echo winexe -U dmn1\\\\#{myID} //#{host} \"cmd\""

Last two lines with printf style:

puts "winexe to host: %s" % host
exec "echo winexe -U dmn1\\\\%s //%s \"cmd\"" % [myID, host]

Last two lines with plus string concatenation:

puts "winexe to host: " + host
exec "echo winexe -U dmn1\\\\" + myID + " //" + host + " \"cmd\""

Last two lines with C++ style append:

puts "winexe to host: " << host
exec "echo winexe -U dmn1\\\\" << myID << " //" << host << " \"cmd\""

3 Comments

this also works, thank you. but im going to use the chained method that Matt showed me above.
@toosweetnitemare: For some reason, I had the idea that I should change your code as little as possible, but that seems like a bad idea now :P
lol. I just taught myself Ruby yesterday afternoon before posting my question so it definitely showed my lack of understanding of syntax in the language. Thank you for taking the time to break down your example into so many different options. That will be very valuable for other users viewing this thread later one. If i could give credit for two answers i would also give it to you. Thank you for your input!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.