Ruby execute bash command with variables

I need to execute the Bash command in a Ruby script. There are approximately 6 ways to do this, according to Nate Murray's “6 Ways to Run Console Commands in Ruby” and several other sources in googled.

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 
+7
source share
2 answers

Why can you really bind these methods, and puts print a new line for you, so it can be simple:

 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 
+4
source

It looks like there might be problems with the way you combined your command line.
In addition, I had to access 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\"" 

The last two lines are of type printf:

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

The last two lines with concatenation plus:

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

The last two lines with the addition of C ++ style:

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

All Articles