[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Assigne command ouput to a variable

Dragoescu Daniel

11/7/2007 8:59:00 AM

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Hello guys!

I am a newbie in ruby and i have a question. I run a system command and i
want that the ouput of that command to be assigne to a variable, something
like this:

var1 = system("du -s /mnt/hdd/ | cut -f1") puts var1 or

var1 = exec("du -s /mnt/hdd/ | cut -f1") puts var1

but system return 0 or 1 if command is failed or succesful, and exec don't
assigne the output to variable var1.

Can you help me? Thanks in advance.



4 Answers

Stefano Crocco

11/7/2007 9:14:00 AM

0

Alle mercoled=EC 7 novembre 2007, Dragoescu Daniel ha scritto:
> Hello guys!
>
> I am a newbie in ruby and i have a question. I run a system command and i
> want that the ouput of that command to be assigne to a variable, something
> like this:
>
> var1 =3D system("du -s /mnt/hdd/ | cut -f1") puts var1 or
>
> var1 =3D exec("du -s /mnt/hdd/ | cut -f1") puts var1
>
> but system return 0 or 1 if command is failed or succesful, and exec don't
> assigne the output to variable var1.
>
> Can you help me? Thanks in advance.

You can use backticks instead of system:

Stefano Crocco

11/7/2007 9:17:00 AM

0

Alle mercoled=EC 7 novembre 2007, Stefano Crocco ha scritto:
> Alle mercoled=EC 7 novembre 2007, Dragoescu Daniel ha scritto:
> > Hello guys!
> >
> > I am a newbie in ruby and i have a question. I run a system command and=
i
> > want that the ouput of that command to be assigne to a variable,
> > something like this:
> >
> > var1 =3D system("du -s /mnt/hdd/ | cut -f1") puts var1 or
> >
> > var1 =3D exec("du -s /mnt/hdd/ | cut -f1") puts var1
> >
> > but system return 0 or 1 if command is failed or succesful, and exec
> > don't assigne the output to variable var1.
> >
> > Can you help me? Thanks in advance.
>
> You can use backticks instead of system:

Sorry, I hit the send button by mistake.

You can use backticks instead of system:

var1 =3D `"du -s /mnt/hdd/ | cut -f1"`
puts var1

I hope this helps

Stefano

Dragoescu Daniel

11/7/2007 9:36:00 AM

0

Hello!

It seems to work with:
----
var1 =3D `du -s /mnt/hdd/ | cut -f1`
puts var1
----
Thank you for the answare!

-----Original Message-----
From: Stefano Crocco [mailto:stefano.crocco@alice.it]=20
Sent: Wednesday, November 07, 2007 11:17 AM
To: ruby-talk ML
Subject: Re: Assigne command ouput to a variable

Alle mercoled=EC 7 novembre 2007, Stefano Crocco ha scritto:
> Alle mercoled=EC 7 novembre 2007, Dragoescu Daniel ha scritto:
> > Hello guys!
> >
> > I am a newbie in ruby and i have a question. I run a system command=20
> > and i want that the ouput of that command to be assigne to a=20
> > variable, something like this:
> >
> > var1 =3D system("du -s /mnt/hdd/ | cut -f1") puts var1 or
> >
> > var1 =3D exec("du -s /mnt/hdd/ | cut -f1") puts var1
> >
> > but system return 0 or 1 if command is failed or succesful, and exec =

> > don't assigne the output to variable var1.
> >
> > Can you help me? Thanks in advance.
>
> You can use backticks instead of system:

Sorry, I hit the send button by mistake.

You can use backticks instead of system:

var1 =3D `"du -s /mnt/hdd/ | cut -f1"`
puts var1

I hope this helps

Stefano



elof

11/7/2007 9:52:00 AM

0


Hi

You can get both the output from the command and its return value:

var1 = `du -s /mnt/hdd/ | cut -f1`
puts $?.exitstatus
puts var1

Kristian


On Wed, 7 Nov 2007 18:35:55 +0900, "Dragoescu Daniel"
<daniel.dragoescu@axigen.com> wrote:
> Hello!
>
> It seems to work with:
> ----
> var1 = `du -s /mnt/hdd/ | cut -f1`
> puts var1
> ----
> Thank you for the answare!
>
> -----Original Message-----
> From: Stefano Crocco [mailto:stefano.crocco@alice.it]
> Sent: Wednesday, November 07, 2007 11:17 AM
> To: ruby-talk ML
> Subject: Re: Assigne command ouput to a variable
>
> Alle mercoledì 7 novembre 2007, Stefano Crocco ha scritto:
>> Alle mercoledì 7 novembre 2007, Dragoescu Daniel ha scritto:
>> > Hello guys!
>> >
>> > I am a newbie in ruby and i have a question. I run a system command
>> > and i want that the ouput of that command to be assigne to a
>> > variable, something like this:
>> >
>> > var1 = system("du -s /mnt/hdd/ | cut -f1") puts var1 or
>> >
>> > var1 = exec("du -s /mnt/hdd/ | cut -f1") puts var1
>> >
>> > but system return 0 or 1 if command is failed or succesful, and exec
>> > don't assigne the output to variable var1.
>> >
>> > Can you help me? Thanks in advance.
>>
>> You can use backticks instead of system:
>
> Sorry, I hit the send button by mistake.
>
> You can use backticks instead of system:
>
> var1 = `"du -s /mnt/hdd/ | cut -f1"`
> puts var1
>
> I hope this helps
>
> Stefano