Shellwords preserve backslash

I am trying to parse a string that is formatted with DOS-like file
names.
For instance:

s = “a b c d “C:\user\sub_dir” “C:\user\other_dir””

When I use Shellwords.shellwords(s) however, I get the following:

Shellwords.shellwords(s)
=> [“a”, “b”, “c”, “d”, “C:usersub_dir”, “C:userother_dir”]

I have also tried Shellwords.escape(s) which yields:

Shellwords.escape(s)
=> “a\ b\ c\ d\ \“C:\\user\\sub_dir\”\
\“C:\\user\\other_dir\””

Which is not parsed correctly by shellwords (I get back a string rather
than an array). What I would like to do is is to preserve the
“C:\user\sub_dir” format and I am stumped on how to do so. I tried a
gsub on the string before sending it to shellwords but that seemed to
preserve nothing. Something similar to

Shellwords.shellwords(s.gsub(’\’, ‘\\\’))
=> [“a”, “b”, “c”, “d”, “C:usersub_dir”, “C:userother_dir”]

It might be of interest that Shellwords.shellwords(s) worked in my
version of 1.8.7. I just updated to the latest stable release (1.9.1)
and ran into this roadblock.
Searching the list logs turned up little.
Any suggestions?

Thanks in advance.

-j b- wrote:

I am trying to parse a string that is formatted with DOS-like file
names.
For instance:

s = “a b c d “C:\user\sub_dir” “C:\user\other_dir””

When I use Shellwords.shellwords(s) however, I get the following:

Shellwords.shellwords(s)
=> [“a”, “b”, “c”, “d”, “C:usersub_dir”, “C:userother_dir”]

I have also tried Shellwords.escape(s) which yields:

Shellwords.escape(s)
=> “a\ b\ c\ d\ \“C:\\user\\sub_dir\”\
\“C:\\user\\other_dir\””

Which is not parsed correctly by shellwords (I get back a string rather
than an array). What I would like to do is is to preserve the
“C:\user\sub_dir” format and I am stumped on how to do so. I tried a
gsub on the string before sending it to shellwords but that seemed to
preserve nothing. Something similar to

Shellwords.shellwords(s.gsub(’\’, ‘\\\’))
=> [“a”, “b”, “c”, “d”, “C:usersub_dir”, “C:userother_dir”]

It might be of interest that Shellwords.shellwords(s) worked in my
version of 1.8.7. I just updated to the latest stable release (1.9.1)
and ran into this roadblock.
Searching the list logs turned up little.
Any suggestions?

Thanks in advance.

I don’t have a solution, but I see the same malfunction moving from ruby
1.8.6 to ruby 1.9.1p243.

r1test.rb:

require ‘shellwords’

s = “a b c d “C:\user\sub_dir” “C:\user\other_dir””
s = s.gsub(’\’, ‘\\\’)
p s

p Shellwords.shellwords(s)

$ ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.11.1]

$ ruby r1test.rb
“a b c d “C:\\user\\sub_dir” “C:\\user\\other_dir””
[“a”, “b”, “c”, “d”, “C:\user\sub_dir”, “C:\user\other_dir”]

$ ruby19 -v
ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-darwin8.11.1]

$ ruby19 r1test.rb
“a b c d “C:\\user\\sub_dir” “C:\\user\\other_dir””
[“a”, “b”, “c”, “d”, “C:usersub_dir”, “C:userother_dir”]

7stud – wrote:

I don’t have a solution, but I see the same malfunction moving from ruby
1.8.6 to ruby 1.9.1p243.

Is this something that should be reported as a bug? If so what is the
appropriate way to do so?
Thanks.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs