How to read output of shell command to variable?

Hi,

In perl, we can do

$output = `file filename.txt`;

How can i do this in Ruby?
I know that there is system to run shell command like this;

system("file","filename.txt")

but how to return the output of "file" command to variable?

Sinchai

Hi --

···

On Sat, 2 Apr 2005, sin kanti wrote:

Hi,

In perl, we can do

$output = `file filename.txt`;

How can i do this in Ruby?
I know that there is system to run shell command like this;

system("file","filename.txt")

but how to return the output of "file" command to variable?

output = `file filename.txt`

David

--
David A. Black
dblack@wobblini.net

$output = `file filename.txt`

:wink:

···

On Friday 01 April 2005 23:52, sin kanti wrote:

Hi,

In perl, we can do

$output = `file filename.txt`;

How can i do this in Ruby?

"Andrew Walrond" <andrew@walrond.org> schrieb im Newsbeitrag news:200504012358.52121.andrew@walrond.org...

···

On Friday 01 April 2005 23:52, sin kanti wrote:

Hi,

In perl, we can do

$output = `file filename.txt`;

How can i do this in Ruby?

$output = `file filename.txt`

:wink:

In fact, you can even add the ";" and keep the Perl code altogether. :-))

$output = `file filename.txt`;

Kind regards

    robert

Robert Klemme wrote:

"Andrew Walrond" <andrew@walrond.org> schrieb im Newsbeitrag
news:200504012358.52121.andrew@walrond.org...

In perl, we can do

$output = `file filename.txt`;

How can i do this in Ruby?

$output = `file filename.txt`

:wink:

In fact, you can even add the ";" and keep the Perl code altogether.
:-))

$output = `file filename.txt`;

I'd prefer Perl's qx:

$output = qx{file filename.txt}

In Ruby that is

output = x{file filename.txt}

Note the absence of the dollar sign at the beginning of the variable
name. The dollar has different meanings in Ruby and Perl.

Josef 'Jupp' Schugt

···

On Friday 01 April 2005 23:52, sin kanti wrote:

I'm pretty sure you meant:

output = %x{file filename.txt}

:slight_smile:

James Edward Gray II

···

On Apr 3, 2005, at 2:56 PM, Josef 'Jupp' Schugt wrote:

I'd prefer Perl's qx:

$output = qx{file filename.txt}

In Ruby that is

output = x{file filename.txt}