inner string to path

G

Gábor SEBESTYÉN

How can I easily conver a string containing path

str =3D "/home/my folder/=E1ccented l=E9tt=E9rs/having (this)"

to one that

`ls #{str}`

UNIX command can understand? It will surely fail at least due to =20
parenthesis and space characters.

G=E1bor


"Earth is a beta site."
 
S

Stefan Lang

How can I easily conver a string containing path

str =3D "/home/my folder/=E1ccented l=E9tt=E9rs/having (this)"

to one that

`ls #{str}`

UNIX command can understand? It will surely fail at least due to
parenthesis and space characters.

Usually it is enough to put quotes around the path:

`ls '#{str}'`

HTH,
Stefan
 
G

Gábor SEBESTYÉN

--Apple-Mail-2-946579287
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=UTF-8;
format=flowed


`ls '#{str}'`
Thanks! It workes with a slight modification: `ls \'#{str}'\`

G=C3=A1bor


"=C3=89lveteg dalnok, kinek dala olvatag, ett=C5=91l lesz a tej =
alvatag!"



--Apple-Mail-2-946579287--
 
R

Robert Klemme

Gábor SEBESTYÉN said:
Thanks! It workes with a slight modification: `ls \'#{str}'\`

Some remarks: you can always use the multi argument form of system:

str = 'your path with strange chars here'
system "ls", str

You can also use Ruby's built in file handling utilities directly to access
file data like size, modification time, directory listings etc. See classes
File and Dir plus the useful package fileutils.

Kind regards

robert
 
K

Kevin Ballard

See above about the multi-argument form, but if you want to stick with
single-argument, you can do something like

foo = `ls \'#{str.gsub("'", "\\'")}\'`

That keeps an internal single-quote from terminating the string.

Alternately, you could do something lik

foo = `ls #{str.gsub(/[" ']/, '\\\\\0')

That escapes spaces and quotes. And if you have LANG (or LC_CTYPE) set
to the right encoding (probably en_US.UTF-8) then the accented
characters will work fine as well.

Note: I needed 5 backslashes in that single-quoted string because of
how gsub works. The string itself evaluates to \\\0, which gsub then
turns into \ followed by the matched string. Not very intuitive, how
gsub compresses backslashes, but I can understand why it does that.
 

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
474,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top