运行带有许多参数的shell命令

时间:2015-02-20 01:39:54

标签: ruby linux shell string-interpolation

我有一个很长的命令,有许多论点,并且不知何故,它的工作方式不起作用。以下knife命令将连接到远程vCenter并创建名为node1的VM。如何包装以下命令并在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)

2 个答案:

答案 0 :(得分:2)

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也可以使用,但比抱歉更安全。

答案 1 :(得分:0)

您的命令中未解析变量。尝试将#{var_name}等用于可变cmd

中的所有变量
相关问题