ruby - 运行带有多个参数的 shell 命令

标签 ruby linux shell string-interpolation

我有一个很长的命令,有很多参数,但不知何故它没有按应有的方式工作。以下 knife 命令将连接到远程 vCenter 并创建一个名为 node1 的虚拟机。如何包装以下命令并在 ruby​​ 中运行?难道我做错了什么?

var_name = 'node1'
var_folder = 'folder1'
var_datastore = 'datastore1'
var_template_file = 'template_foo'
var_template = 'foo'
var_location = 'US'

cmd = 'knife vsphere vm clone var_name --dest-folder var_folder --datastore var_datastore --template-file var_template_file --template var_template -f var_location'

system(cmd)

最佳答案

require 'shellwords'
cmd = "knife vsphere vm clone #{var_name.shellescape} --dest-folder #{var_folder.shellescape} --datastore #{var_datastore.shellescape} --template-file #{var_template_file.shellescape} --template #{var_template.shellescape} -f #{var_location.shellescape}"

在您的具体情况下,即使没有shellescape,它也可以工作,但安全总比遗憾好。

关于ruby - 运行带有多个参数的 shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28620323/

相关文章:

ruby-on-rails - 如何测试 ruby​​ on rails 和 assert_select 中的禁用字段?

ruby 。 Selenium 。代理人。代理不工作

c - Linux 中出现段错误,但在 Mac 中正常

linux - 如何从双跳 SFTP 下载?

shell - 在 AWK 中运行 shell 命令

linux - shell 清理脚本建议

ruby - 如何在 ruby​​ 中迭代多个枚举?

ruby-on-rails - Rails 匹配 validates_format_of 数字 1-9

linux - Linux ARM 上程序寄存器和堆栈的初始状态

mysql - 无法使用shell脚本连接mysql数据库