[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

escape special charactor

sin kanti

4/25/2005 6:56:00 PM

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



3 Answers

Joel VanderWerf

4/25/2005 7:48:00 PM

0

sin kanti wrote:
> 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


sin kanti

4/25/2005 7:55:00 PM

0

i use
$output = `command`
because i want to puts $output later

sinchai

On 4/25/05, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> sin kanti wrote:
> > 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
>
>



Robert Klemme

4/26/2005 7:59:00 AM

0


"sin kanti" <sinkanti@gmail.com> schrieb im Newsbeitrag
news:377080260504251254103e737b@mail.gmail.com...
> 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

>
> sinchai
>
> On 4/25/05, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> > sin kanti wrote:
> > > 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
> >
> >
>
>
>