From: Damjan Rems on
Marc Weber wrote:
> Learn about Dir object. It has methods such as glob which should give
> you directory contents. Then you don't have to care about stdout, nor
> quoting.
>
> http://whynotwiki.com/Comparison_of_Escape_class_and_String.shell_escape
>
> Have a look at the first example here
>
> Marc

Thanks guys.

I used ls just as an example. I am actually calling smartctl command to
find out SMART status of all disks on my computer.

by
TheR
--
Posted via http://www.ruby-forum.com/.

From: Schneider on
On Apr 9, 8:46 am, Albert Schlef <albertsch...(a)gmail.com> wrote:
> Damjan Rems wrote:
>
> > You can run linux command:
>
> > a = `ls /home`
>
> > and receive output in variable a.
>
> > But I would like /home to be variable. So I would invoke command like
> > this:
>
> > a = `"ls #{dir}"`
>
> As Ryan said, you need to remove the double quotes. You're asking the
> shell to execute the "ls whatever" command. There's no such command.
>
> BTW, you actually want double quotes there, but put them around the
> parameter:
>
>   a = `ls "#{dir}"`
>
> The purpose of the double quotes here is to protect against the case
> where the 'dir' variable contain spaces (which are meaningful to the
> shell --they separate tokens).
> --
> Posted viahttp://www.ruby-forum.com/.

Keep in mind that `ls #{dir}` (without the quotes) is also DANGEROUS.
What if you (or someone) set dir = "~; rm -rf ~"? BAD.

So, using quotes is also safer.
From: Albert Schlef on
Schneider wrote:
> On Apr 9, 8:46�am, Albert Schlef <albertsch...(a)gmail.com> wrote:
>>
>> The purpose of the double quotes here is to protect against the case
>> where the 'dir' variable contain spaces (which are meaningful to the
>> shell --they separate tokens).
>> --
>> Posted viahttp://www.ruby-forum.com/.
>
> Keep in mind that `ls #{dir}` (without the quotes) is also DANGEROUS.
> What if you (or someone) set dir = "~; rm -rf ~"? BAD.
>
> So, using quotes is also safer.

No, using quotes isn't safer:

dir = '123" ; rm -rf ~ ; echo "'
`ls "#{dir}"`

Results it doing ls "123" ; rm -rf ~ ; echo ""
--
Posted via http://www.ruby-forum.com/.