2014-10-16 63 views
2

我有类似于Ruby脚本下面的内容:红宝石系统调用CP:缺少目标文件操作后

from = "/path/to/script.rb" 
to = "/path/to/destination/" 
puts "Copying #{from} to #{to}" 
system("cp #{from} #{to}") 

这使得在终端输出如下:

copying /path/to/script.rb 
to /path/to/destination/ 
cp: missing destination file operand after ‘/path/to/script.rb’ 
Try 'cp --help' for more information. 

我没有在终端运行以下问题:

cp /path/to/script.rb /path/to/destination 

也不在红宝石脚本中:

system("cp /path/to/script.rb /path/to/destination") 

任何意见,如何更好地排查我的脚本或指针在正确的方向将不胜感激。

+0

不'系统( 'CP',从,到)'工作? – 2014-10-16 18:41:47

+0

问题是我正在从一个文件中读取,而不是修剪换行符。我只是编辑输出来说明。 – 2014-10-16 18:44:17

+0

您应该回答自己的问题并将其标记为已接受。 – 2014-10-16 18:45:46

回答

2

我正在从一个文件中读取,而不是修剪换行符。这个应该已经从终端输出中显而易见(注意'到'之前的换行符和空格)。

下更改:

files = [] 
File.open(file-of-paths).read.each_line do |line| 
files.push(line) 
end 

下面的解决了这个问题:

files = [] 
File.open(file-of-paths).read.each_line do |line| 
files.push(line.chomp) 
end 
+1

您应该使用['File.foreach'](http://www.ruby-doc.org/core-2.1.3/IO.html#method-c-foreach)为了遍历行。它更高效,简洁,并在完成后关闭文件。 – 2014-10-16 18:51:15

+0

感谢您的提示!更新了我的脚本。 – 2014-10-16 19:38:30