how to read output of shell command to variable?

S

sin kanti

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
 
D

David A. Black

Hi --

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
 
R

Robert Klemme

Andrew Walrond said:
$output = `file filename.txt`

;)

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

$output = `file filename.txt`;

Kind regards

robert
 
J

Josef 'Jupp' Schugt

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Robert Klemme wrote:
|
| |
|> On Friday 01 April 2005 23:52, sin kanti wrote:
|>> In perl, we can do
|>>
|>> $output = `file filename.txt`;
|>>
|>> How can i do this in Ruby?
|>
|> $output = `file filename.txt`
|>
|> ;)
|
| 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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCUBldrhv7B2zGV08RAh8/AJ4xXeLsNXniiR/8bFbVnzo5ZIDcyQCgp0wQ
Fv/VUex4eyIgthTEjvzdAf4=
=moeo
-----END PGP SIGNATURE-----
 
J

James Edward Gray II

I'd prefer Perl's qx:

$output = qx{file filename.txt}

In Ruby that is

output = x{file filename.txt}

I'm pretty sure you meant:

output = %x{file filename.txt}

:)

James Edward Gray II
 

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,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top