Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby按扩展名复制文件_Ruby_Chef Infra_Chef Recipe - Fatal编程技术网

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) 很

我试图从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)


很可能您的
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。这很好!感谢您的支持。