As an example i want to run the following command under a rake.
robocopy C:\Media \\other\Media /mir
The rakefile i was able to get working was
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 the handling of the string literal is awkward.
If i use a heredoc to enter the string literal
<<HEREDOC
copy C:\Media \\other\Media /mir
HEREDOC
i get the 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 back slashes gets lost.
irb(main):001:0> 'copy C:\Media \\other\Media /mir'
=> "copy C:\\Media \\other\\Media /mir"
Double backslash is interpreted as an escaped single backslash. You should escape each backslash in the string.
irb(main):001:0> puts 'robocopy C:\\Media \\\\other\\Media /mir'
robocopy C:\Media \\other\Media /mir
Or, if you really don't want to escape the backslashes, you can use a here doc with a single quoted identifier.
irb(main):001:0> <<'HEREDOC'
irb(main):002:0' copy C:\Media \\other\Media /mir
irb(main):003:0' HEREDOC
=> "copy C:\\Media \\\\other\\Media /mir\n"
irb(main):004:0> puts _
copy C:\Media \\other\Media /mir
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With