Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby execute bash command with variables

Tags:

bash

command

ruby

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 
like image 725
toosweetnitemare Avatar asked Feb 03 '23 11:02

toosweetnitemare


1 Answers

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
like image 197
Matt Sanders Avatar answered Feb 12 '23 13:02

Matt Sanders