我需要在Ruby脚本中执行Bash命令.根据Nate Murray和其他一些谷歌来源的"6种方式在Ruby中运行Shell命令",大约有6种方法可以做到这一点.
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 
Run Code Online (Sandbox Code Playgroud)
    对于它的价值,您可以实际链接这些方法,并puts为您打印换行符,所以这可能只是:
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
Run Code Online (Sandbox Code Playgroud)
        看起来你的命令字符串放在一起可能有问题.
另外,我不得不直接参考STDIN.
# 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
Run Code Online (Sandbox Code Playgroud)
紧凑版:
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\""
Run Code Online (Sandbox Code Playgroud)
最后两行printf样式:
puts "winexe to host: %s" % host
exec "echo winexe -U dmn1\\\\%s //%s \"cmd\"" % [myID, host]
Run Code Online (Sandbox Code Playgroud)
最后两行加上字符串连接:
puts "winexe to host: " + host
exec "echo winexe -U dmn1\\\\" + myID + " //" + host + " \"cmd\""
Run Code Online (Sandbox Code Playgroud)
最后两行带有C++样式的行追加:
puts "winexe to host: " << host
exec "echo winexe -U dmn1\\\\" << myID << " //" << host << " \"cmd\""
Run Code Online (Sandbox Code Playgroud)