Capturing shell command output and success?

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

and of course, that's a typeo, it's $?, not @?

···

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

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

simplest and most portable:

gem install systemu

status, stdout, stderr = systemu command

a @ http://codeforpeople.com/

···

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

--
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,
        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 :slight_smile:

--Kyle

···

On Fri, Aug 1, 2008 at 2:54 PM, Ståle Z.H <szerener@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.

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.)

···

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&#39;s just

easier to read :slight_smile:

--Kyle

Right, but standard error goes into lala land.

I was hoping for something a bit cleaner than executing with %x then
checking the $? value.

I may try this systemu command...

···

On Fri, Aug 1, 2008 at 3:19 PM, Ståle Z.H <szerener@gmail.com> wrote:

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.)

fyi - it also works on windoze...

a @ http://codeforpeople.com/

···

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

I may try this systemu command...

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

Just call,

system(cmd)

it returns true if success or false if doesn´t.

···

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

Sorry I didn´t read before.

···

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

Just call,

system(cmd)

it returns true if success or false if doesn´t.

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

--
Pablo Q.