Shellwords preserve backslash

J

-j b-

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.
 
7

7stud --

-j b- said:
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"]
 
J

-j b-

7stud said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,834
Latest member
BuyCannaLabsCBD

Latest Threads

Top