在Ruby中使用filename中的空格执行脚本

时间:2013-04-10 14:57:24

标签: ruby

我编写了一个简单的脚本来检查Dropbox文件的状态并将其移动到另一个文件夹,如果该文件已经完全同步并下载(状态=='最新')。

我现在唯一的问题是,如果文件或目录中有空格,那么我的脚本将无法正常运行。

我使用#{...}来传递文件名,但似乎它会拆分在空格处给它的任何内容。

这是我的剧本:

# runs in Ruby 1.8.x (ftools)                                                                                    

 class DropboxHelper                                                                  

     require 'ftools'                                                                 

     def self.move                                                                    
         directory = "Foo"                                                     
         destination = "Bar"                                                   

         while true                                                                   
             Dir.glob(directory+"/**/*") do |item|                                    
                 next if item == '.' or item == '..'                                  
                 fileStatus = `~/bin/dropbox.py filestatus #{item}`                   
                 puts "processing " + item                                            
                 puts "filestatus: " + fileStatus                                     
                 if (fileStatus.include? "up to date")                                
                     puts item.split('/',2)[1] + " is up to date, starting to move file now."
                     `cp -r #{item + " " + destination + "/" + item.split('/',2)[1]} >> copiedFile.out`

                     # remove file in Dropbox folder, if current item is not a directory and 
                     # copied file is the identical.                                  
                     if (!File.directory?(item) && File.cmp(item, destination + "/" + item.split('/',2)[1]).to_s)
                         puts "remove " + item                                        
                         `rm -rf #{item} >> removedFile.out`                          
                     end                                                              
                 else                                                                 
                     puts item + " is not up to date, moving to next file."           
                 end                                                                  
             end                                                                      

             # execute every hour                                                     
             puts "sleeping now"                                                      
             sleep(3600)                                                              
         end                                                                          
     end                                                                              
 end                                                                                  

 DropboxHelper.move   

当运行脚本时,我得到以下输出,如果文件或目录中有空格。它甚至说文件不是最新的,尽管它是:

Foo/Name with spaces is not up to date, moving to next file.
processing Foo/Name with spaces/file #1.txt
filestatus: Foo/Name: File doesn't exist
with:                 File doesn't exist
spaces/file:          File doesn't exist
Foo/Name with spaces/file #1.txt is not up to date, moving to next file.

有关如何改进和修复代码的任何建议吗?

2 个答案:

答案 0 :(得分:5)

#{item}这样的单引号包装'#{item}'的所有出现。例如:

fileStatus = `~/bin/dropbox.py filestatus '#{item}'`

你需要这样做,因为bash(或你正在使用的任何shell)以特殊方式解释空格。由于您的文件名本身包含空格,因此您需要将其转义。有关详情,请参阅3.3. Quoting characters中的Bash Guide for Beginners

答案 1 :(得分:0)

您可以使用内置shellescape

fileStatus = `~/bin/dropbox.py filestatus #{item.shellescape}`

http://ruby-doc.org/stdlib-2.3.0/libdoc/shellwords/rdoc/Shellwords.html#method-c-shellescape