escape special charactor

S

sin kanti

Hi all,

I write script to send password to unix shell.
But i found out that if the password contain special charactor such as &,
Shell will interpret it as background process.
So, it have to use \& instead.

Is there any function can detect special char?
and return it is escape form? ( like \& )

i found similar function in Regexp.escape, but it can't detect &

any idea?

sinchai
 
J

Joel VanderWerf

sin said:
Hi all,

I write script to send password to unix shell.
But i found out that if the password contain special charactor such as &,
Shell will interpret it as background process.
So, it have to use \& instead.

Is there any function can detect special char?
and return it is escape form? ( like \& )

i found similar function in Regexp.escape, but it can't detect &

Are you using #system? If so, you can prevent the shell from expanding
things by passing each shell argument as a separate ruby argument:

irb(main):001:0> system "echo foo&"
foo
=> true
irb(main):002:0> system "echo", "foo&"
foo&
=> true
 
R

Robert Klemme

sin kanti said:
i use
$output = `command`
because i want to puts $output later

Two options: use single quotes when building the command like:

cmd = "your_program -p '#{password}'"

escape the password:

cmd = "your_program -p #{password.gsub(/&/, '\\\\&')}"

I'd choose optin 1 because it's simpler and safer (note though that you
need similar escaping if the password contains single quotes).

Kind regards

robert
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top