[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Capturing shell command output and success?

Kyle Schmitt

8/1/2008 7:33:00 PM

I know shell commands have beaten to death on this list, but searching
the archives I couldn't quite find what I was looking for.

I want a way to capture the output of the shell command, and the
success or failure of it.

Of course I've used system("") and %x, and I have been successful in
getting the return code using @?, although something about it rubs me
the wrong way. Not the least of which is that standard error isn't
captured by anything.

In the case of what I'm doing now, I'd like to shell out to tar, have
an error returned to me if tar doesn't complete successfully, AND have
the output of tar for parsing.

Is there a clean mechanism to do this?

Thanks
--Kyle

23 Answers

Kyle Schmitt

8/1/2008 7:46:00 PM

0

On Fri, Aug 1, 2008 at 2:33 PM, Kyle Schmitt <kyleaschmitt@gmail.com> wrote:
> getting the return code using @?, although something about it rubs me
and of course, that's a typeo, it's $?, not @?

Ståle Z.H

8/1/2008 7:52:00 PM

0

Not quite sure if I understood you right, but you could probably do
something like this:
res = `command` # note: ` not '

This executes 'command' and stores the output in res.

Kyle Schmitt skrev:
> I know shell commands have beaten to death on this list, but searching
> the archives I couldn't quite find what I was looking for.
>
> I want a way to capture the output of the shell command, and the
> success or failure of it.
>
> Of course I've used system("") and %x, and I have been successful in
> getting the return code using @?, although something about it rubs me
> the wrong way. Not the least of which is that standard error isn't
> captured by anything.
>
> In the case of what I'm doing now, I'd like to shell out to tar, have
> an error returned to me if tar doesn't complete successfully, AND have
> the output of tar for parsing.
>
> Is there a clean mechanism to do this?
>
> Thanks
> --Kyle

Kyle Schmitt

8/1/2008 7:59:00 PM

0

T24gRnJpLCBBdWcgMSwgMjAwOCBhdCAyOjU0IFBNLCBTdMOlbGUgWi5IIDxzemVyZW5lckBnbWFp
bC5jb20+IHdyb3RlOgo+IE5vdCBxdWl0ZSBzdXJlIGlmIEkgdW5kZXJzdG9vZCB5b3UgcmlnaHQs
IGJ1dCB5b3UgY291bGQgcHJvYmFibHkgZG8KPiBzb21ldGhpbmcgbGlrZSB0aGlzOgo+IHJlcyA9
IGBjb21tYW5kYCAgIyBub3RlOiBgIG5vdCAnCj4KPiBUaGlzIGV4ZWN1dGVzICdjb21tYW5kJyBh
bmQgc3RvcmVzIHRoZSBvdXRwdXQgaW4gcmVzLgpTdMOlbGUsCiAgICAgICAgVGhlIHByb2JsZW0g
d2l0aCB0aGF0IGlzIHRoYXQgaXQgZG9lc24ndCBpbmRpY2F0ZSB3aGV0aGVyIHRoZQpjb21tYW5k
IHN1Y2NlZWRlZCBvciBmYWlsZWQuCgogICAgICAgICBCeSB0aGUgd2F5LCAleHtscyAvdG1wfSBp
cyBpZGVudGljYWwgdG8gYGxzIC90bXBgLCBpdCdzIGp1c3QKZWFzaWVyIHRvIHJlYWQgOikKCi0t
S3lsZQo=

ara.t.howard

8/1/2008 8:00:00 PM

0


On Aug 1, 2008, at 1:32 PM, Kyle Schmitt wrote:

> I know shell commands have beaten to death on this list, but searching
> the archives I couldn't quite find what I was looking for.
>
> I want a way to capture the output of the shell command, and the
> success or failure of it.
>
> Of course I've used system("") and %x, and I have been successful in
> getting the return code using @?, although something about it rubs me
> the wrong way. Not the least of which is that standard error isn't
> captured by anything.
>
> In the case of what I'm doing now, I'd like to shell out to tar, have
> an error returned to me if tar doesn't complete successfully, AND have
> the output of tar for parsing.
>
> Is there a clean mechanism to do this?
>
> Thanks
> --Kyle

simplest and most portable:

gem install systemu

status, stdout, stderr = systemu command

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Ståle Z.H

8/1/2008 8:16:00 PM

0

On 1 Aug, 21:59, Kyle Schmitt <kyleaschm...@gmail.com> wrote:
> On Fri, Aug 1, 2008 at 2:54 PM, Ståle Z.H <szere...@gmail.com> wrote:
> > Not quite sure if I understood you right, but you could probably do
> > something like this:
> > res = `command`  # note: ` not '
>
> > This executes 'command' and stores the output in res.
>
> Ståle,
>         The problem with that is that it doesn't indicate whether the
> command succeeded or failed.
>
>          By the way, %x{ls /tmp} is identical to `ls /tmp`, it's just
> easier to read :)
>
> --Kyle

The return value of the program is stored in $? as far as I can see. I
tested it on my laptop, and $? was set to 0 when the command succeeded
and 1 when it didn't. (Of course, the return value depends on the
program, but non-zero is generally considered failure.)

Kyle Schmitt

8/1/2008 9:35:00 PM

0

T24gRnJpLCBBdWcgMSwgMjAwOCBhdCAzOjE5IFBNLCBTdMOlbGUgWi5IIDxzemVyZW5lckBnbWFp
bC5jb20+IHdyb3RlOgo+IFRoZSByZXR1cm4gdmFsdWUgb2YgdGhlIHByb2dyYW0gaXMgc3RvcmVk
IGluICQ/IGFzIGZhciBhcyBJIGNhbiBzZWUuIEkKPiB0ZXN0ZWQgaXQgb24gbXkgbGFwdG9wLCBh
bmQgJD8gd2FzIHNldCB0byAwIHdoZW4gdGhlIGNvbW1hbmQgc3VjY2VlZGVkCj4gYW5kIDEgd2hl
biBpdCBkaWRuJ3QuIChPZiBjb3Vyc2UsIHRoZSByZXR1cm4gdmFsdWUgZGVwZW5kcyBvbiB0aGUK
PiBwcm9ncmFtLCBidXQgbm9uLXplcm8gaXMgZ2VuZXJhbGx5IGNvbnNpZGVyZWQgZmFpbHVyZS4p
CgpSaWdodCwgYnV0IHN0YW5kYXJkIGVycm9yIGdvZXMgaW50byBsYWxhIGxhbmQuCgpJIHdhcyBo
b3BpbmcgZm9yIHNvbWV0aGluZyBhIGJpdCBjbGVhbmVyIHRoYW4gZXhlY3V0aW5nIHdpdGggJXgg
dGhlbgpjaGVja2luZyB0aGUgJD8gdmFsdWUuCgoKSSBtYXkgdHJ5IHRoaXMgc3lzdGVtdSBjb21t
YW5kLi4uCg==

ara.t.howard

8/1/2008 10:46:00 PM

0


On Aug 1, 2008, at 3:34 PM, Kyle Schmitt wrote:

> I may try this systemu command...



fyi - it also works on windoze...

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Pablo Q.

8/6/2008 6:28:00 PM

0

Just call,

system(cmd)

it returns true if success or false if doesn=B4t.

2008/8/1 ara.t.howard <ara.t.howard@gmail.com>

>
> On Aug 1, 2008, at 3:34 PM, Kyle Schmitt wrote:
>
> I may try this systemu command...
>>
>
>
>
> fyi - it also works on windoze...
>
>
> a @ http://codeforp...
> --
> we can deny everything, except that we have the possibility of being
> better. simply reflect on that.
> h.h. the 14th dalai lama
>
>
>
>
>


--=20
Pablo Q.

Pablo Q.

8/6/2008 6:36:00 PM

0

Sorry I didn=B4t read before.

2008/8/6 Pablo Q. <paqs140482@gmail.com>

> Just call,
>
> system(cmd)
>
> it returns true if success or false if doesn=B4t.
>
> 2008/8/1 ara.t.howard <ara.t.howard@gmail.com>
>
>
>> On Aug 1, 2008, at 3:34 PM, Kyle Schmitt wrote:
>>
>> I may try this systemu command...
>>>
>>
>>
>>
>> fyi - it also works on windoze...
>>
>>
>> a @ http://codeforp...
>> --
>> we can deny everything, except that we have the possibility of being
>> better. simply reflect on that.
>> h.h. the 14th dalai lama
>>
>>
>>
>>
>>
>
>
> --
> Pablo Q.
>



--=20
Pablo Q.

David Hartung

10/21/2011 10:08:00 AM

0

On 10/20/2011 11:57 PM, 6280 Dead, 1423 since 1/20/09 wrote:
> On Thu, 20 Oct 2011 23:30:24 -0400, Vandar wrote:
>
>> 6280 Dead, 1423 since 1/20/09 wrote:
>>
>>> On Thu, 20 Oct 2011 18:54:48 -0400, Vandar wrote:
>>>
>>>
>>>> 6280 Dead, 1423 since 1/20/09 wrote:
>>>>
>>>>
>>>>> On Thu, 20 Oct 2011 16:28:13 -0400, Vandar<vandar69@yahoo.com> wrote:
>>>>>
>>>>>
>>>>>
>>>>>> wy wrote:
>>>>>>
>>>>>>
>>>>>>
>>>>>>> On Oct 20, 3:32 pm, Vandar<vanda...@yahoo.com> wrote:
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>> 6280 Dead, 1423 since 1/20/09 wrote:
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>> On Thu, 20 Oct 2011 10:06:15 -0700 (PDT), wy<w...@myself.com>
>>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>>> On Oct 20, 12:20 pm, David Hartung<david@hotmai*l.com> wrote:
>>>>>>>>
>>>>>>>>>>> On 10/20/2011 10:50 AM, 6280 Dead, 1423 since 1/20/09 wrote:
>>>>>>>>
>>>>>>>>>>>> On Thu, 20 Oct 2011 10:43:39 -0500, David Hartung wrote:
>>>>>>>>
>>>>>>>>>>>>> What troubles me about this issue is that NATO and the USA
>>>>>>>>>>>>> seem to have developed a policy ov intervening in internal
>>>>>>>>>>>>> fights. Libya has never been a threat to NATO, and yet NATO
>>>>>>>>>>>>> found it necessary to enter the fight.
>>>>>>>>
>>>>>>>>>>>>> Yes, Libya supported the Pan-Am bomber, but as I recall, he
>>>>>>>>>>>>> was caught, and imprisoned for his crime. Was our intervention
>>>>>>>>>>>>> truly called for?
>>>>>>>>
>>>>>>>>>>>> Yeah, what DO you think of this notion that western nations
>>>>>>>>>>>> have the right to overthrow and kill foreign leaders of whom
>>>>>>>>>>>> they don't approve?
>>>>>>>>
>>>>>>>>>>> To what, specifically, are you referring?
>>>>>>>>
>>>>>>>>>>>> Something to keep in mind if the Canadian government is
>>>>>>>>>>>> compelled to arrest George W. Bush on war crime charges.
>>>>>>>>
>>>>>>>>>>> Which will not happen. For Canada to do such, would constitute,
>>>>>>>>>>> in my opinion, an act of war.
>>>>>>>>
>>>>>>>>>> Bush is a private citizen not worth going to war over. Get real
>>>>>>>>>> some day, will you?
>>>>>>>>
>>>>>>>>> Good luck getting the US population to chant "Free George or
>>>>>>>>> Fight!"
>>>>>>>>
>>>>>>>>> I think quite a few Americans would consider the arrest justified.
>>>>>>>>
>>>>>>>> And the people that matter would consider it a violation of US law.
>>>>>>>
>>>>>>>
>>>>>>> What's the law and how does it trump Canadian law? Your laws begin
>>>>>>> and end at your borders.
>>>>>>
>>>>>> Then I guess your Canadian laws don't apply to the actions of a US
>>>>>> citizen regarding Iraq. Canada has no jurisdiction.
>>>>>
>>>>>
>>>>> Canada by treaty is a signator to international law, which DOES have
>>>>> jurisdiction.
>>>>
>>>> Not over Americans, it doesn't.
>>>
>>>
>>> Yes, it does. Oh, don't worry; they can't come in American territory
>>> and snatch war criminals off the streets. But they can arrest and try
>>> Americans who stray into nations that honor their treaties.
>>
>> Not according to US law, they can't. But it's irrelevant, the Canadian
>> government has already such talk is meaningless. Besides, Bush is
>> already there. Already gave his speech. He's probably headed home by
>> now.
>> It's over. Done. Does not apply.
>
> Bubbles, try to understand: US law doesn't apply in Canada. It's a
> different country.
>
> Now, personally, I'm sorry Canada didn't arrest his murderous ass, but I
> knew that Harper and his government are pretty deficient, so I didn't
> have high hopes.
>
> But know this: America is regarded as a rogue nation by much of the
> civilized world.

Then you are more than welcome to leave.

IN the fifteen years I have been in and out of this forum, you have done
nothing but denigrate the country in which you live, a country in which
you are not a citizen. Why on earth do you stay? If we are so very bad,
why haven't you left?