我有一个很长的命令,有很多参数,但不知何故它没有按应有的方式工作。以下 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/