ruby用变量执行bash命令

时间:2021-12-12 15:45:33

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.

我需要在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 

2 个解决方案

#1


4  

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

对于它的价值,您可以实际链接这些方法,并且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

#2


5  

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.

看起来你的命令字符串放在一起可能会遇到麻烦。另外,我不得不直接参考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



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:

最后两行printf样式:

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:

附加了C ++样式的最后两行:

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

#1


4  

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

对于它的价值,您可以实际链接这些方法,并且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

#2


5  

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.

看起来你的命令字符串放在一起可能会遇到麻烦。另外,我不得不直接参考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



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:

最后两行printf样式:

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:

附加了C ++样式的最后两行:

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