Ruby按扩展名复制文件
我试图从Chef处执行下面的命令Ruby块,并给出下面的错误Ruby按扩展名复制文件,ruby,chef-infra,chef-recipe,Ruby,Chef Infra,Chef Recipe,我试图从Chef处执行下面的命令Ruby块,并给出下面的错误 FileUtils.cp_r Dir.glob "#{node['default']['home']}/standalone/deployments/'*.ear'", "#{node['default']['default_backup_path']}/my_bkp_#{time}", :noop => true, :verbose => true ArgumentError:参数数目错误(给定3个,应为1..2) 很
FileUtils.cp_r Dir.glob "#{node['default']['home']}/standalone/deployments/'*.ear'", "#{node['default']['default_backup_path']}/my_bkp_#{time}", :noop => true, :verbose => true
ArgumentError:参数数目错误(给定3个,应为1..2)
很可能您的
Dir.glob
方法需要括号,它只接受传递的第一个参数,其余的被视为FileUtils.cp\r
参数,请尝试:
FileUtils.cp_r(
Dir.glob(
"#{node['default']['home']}/standalone/deployments/'*.ear'",
"#{node['default']['default_backup_path']}/my_bkp_#{time}"
),
'destination/',
noop: true,
verbose: true
)
您正在向glob
方法传递两个patterns
参数,这就是错误的原因:
没有将字符串隐式转换为整数(TypeError)
尝试对每个模式使用cp\r
,如:
FileUtils.cp_r(
Dir.glob("#{node['default']['home']}/standalone/deployments/'*.ear'"),
'destination/',
noop: true,
verbose: true
)
FileUtils.cp_r(
Dir.glob("#{node['default']['default_backup_path']}/my_bkp_#{time}"),
'destination/',
noop: true,
verbose: true
)
我认为您的目标是迭代以获取元素
“#{node['default']['home']}/standalone/deployments/'*.ear'”
“{node['default']['default\u backup\u path']}/my\u bkp\u35;{time}”
Dir.glob
方法中使用此模式,如:
patterns = [
"#{node['default']['home']}/standalone/deployments/'*.ear'",
"#{node['default']['default_backup_path']}/my_bkp_#{time}"
]
patterns.each do |pattern|
FileUtils.cp_r Dir.glob(pattern), 'destination/', noop: true, verbose: true
end
其中,'destination/'
是您缺少的将在其中复制元素的文件夹
或者,如果您想使用default\u backup\u path
文件夹作为目标,则不需要Dir.glob
方法,只需将其作为dest
参数添加为字符串即可
FileUtils.cp_r(
Dir.glob("#{node['default']['home']}/standalone/deployments/'*.ear'"),
"#{node['default']['default_backup_path']}/my_bkp_#{time}",
noop: true,
verbose: true
)
谢谢你的建议。我试过这个,它给了我一个错误,说“hadaerror:TypeError:no implicit conversion of String to Integer”,所以,这个错误已经改变了,它不告诉你是哪一行出错了吗?是的。但它是相同的行号。我对Ruby不熟悉。所以有点纠结于此。非常感谢@Sebastian。这很好!感谢您的支持。