Capturing shell command output and success?

K

Kyle Schmitt

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
 
S

Ståle Z.H

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:
 
K

Kyle Schmitt

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

ara.t.howard

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://codeforpeople.com/
 
S

Ståle Z.H

Ståle,
        The problem with that is that it doesn't indicate whetherthe
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.)
 
K

Kyle Schmitt

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

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top