How to capture output of linux command

D

Damjan Rems

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}"`

This of course doesn't work, but I hope you get the point.

a = system("ls #{dir}") works, but returns only true or false not the
standard output of command.

How can I get standard output of the command and use variable as
parameter?


by
TheR
 
A

Albert Schlef

Damjan said:
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).
 
M

Marc Weber

Excerpts from Albert Schlef's message of Fri Apr 09 13:46:34 +0200 2010:
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).

If we got hat route you want ls -- ${dir} to protect against directories
called "-a" or such .. And keep in mind that directories may contain
slashes and such stuff on linux. eg try mkdir a\\b
thus using quotes is not enough. It may work for most cases you use
though. Anyway I'd do it right - you never know.

Marc Weber
 
S

Schneider

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

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

Albert Schlef

Schneider said:
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 ""
 

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,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top