As an example, I want to run the following command under a rake.
robocopy C:\Media \\other\Media /mir
In rakefile I was able to get a job
def sh(str)
str.tr!('|', '\\')
IO.popen(str) do |pipe|
pipe.each do |line|
puts line
end
end
end
task :default do
sh 'robocopy C:|Media ||other|Media /mir'
end
However, processing a string literal is inconvenient.
If I use heredoc to enter a string literal
<<HEREDOC
copy C:\Media \\other\Media /mir
HEREDOC
I get an error
rakefile.rb:15: Invalid escape character syntax
copy C:\Media \\other\Media /mir
^
rakefile.rb:15: Invalid escape character syntax
copy C:\Media \\other\Media /mir
^
If I use single quotes, one of the backslashes disappears.
irb(main):001:0> 'copy C:\Media \\other\Media /mir'
=> "copy C:\\Media \\other\\Media /mir"
Frank source
share