Unix questions - Capture unix command shell output to a buffer

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > January 2005 > Capture unix command shell output to a buffer





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Capture unix command shell output to a buffer
New User

2004-12-22, 5:54 pm

Hello All,

I searched for this for long time but no luck.

Basically, I need to capture the shell command output to a buffer not
to a file. For example, I will have buffer, char output[100]; and I
want to capture the result of 'ls' command in that buffer. I am doing
this on read only file system so I don't have luxury to create a file.

Is this possible? .. if so, then can some tell me how?
Any help is much appreciated.

Barry Margolin

2004-12-22, 8:54 pm

In article <1103754826.982519.144770@c13g2000cwb.googlegroups.com>,
"New User" <webcontacts00@yahoo.com> wrote:

> Hello All,
>
> I searched for this for long time but no luck.
>
> Basically, I need to capture the shell command output to a buffer not
> to a file. For example, I will have buffer, char output[100]; and I
> want to capture the result of 'ls' command in that buffer. I am doing
> this on read only file system so I don't have luxury to create a file.
>
> Is this possible? .. if so, then can some tell me how?
> Any help is much appreciated.


man popen

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
New User

2004-12-27, 5:52 pm

Hi,

I tried this:

FILE *fp;
char cmd[10];
zero(cmd); // metset cmd buffer to 0x0
strcpy(cmd, "ls -al");
char cmd_result[1000];
zero(cmd_result); // memset cmd_result to 0x0

fp = popen(cmd, "r");

while (fgets(cmd_result, sizeof(cmd_result), fp))
{
printf("%s", cmd_result);
}

pclose(fp);

But when I display cmd_result, it only shows the last line of the
command output.. .. any idea why?

Thanks,

Arto Viitanen

2004-12-28, 2:51 am

>>>>> "New" == New User <webcontacts00@yahoo.com> writes:

New> Hi, I tried this:

New> FILE *fp;
New> char cmd[10];
New> zero(cmd); // metset cmd buffer to 0x0
New> strcpy(cmd, "ls -al");
New> char cmd_result[1000];
New> zero(cmd_result); // memset cmd_result to 0x0

New> fp = popen(cmd, "r");

New> while (fgets(cmd_result, sizeof(cmd_result), fp))
New> {
New> printf("%s", cmd_result);
New> }

New> pclose(fp);

New> But when I display cmd_result, it only shows the last line of the
New> command output.. .. any idea why?

I tried your program (without the zero() calls) and it run the ls -al and
showed the listing. Are you perhaps asking, why the cmd_result contains only
the last line after the pclose(fp)? Well it is, since each fgets() drops the
previous content, so what is left on the cmd_result is the last line.

You should use strcat or something similar to collect the output.

--
Arto V. Viitanen av@cs.uta.fi
University of Tampere, Department of Computer Sciences
Tampere, Finland http://www.cs.uta.fi/~av/
New User

2005-01-03, 2:48 am

Hi,

I tried this:

FILE *fp;
char cmd[10];
zero(cmd); // metset cmd buffer to 0x0
strcpy(cmd, "ls -al");
char cmd_result[1000];
zero(cmd_result); // memset cmd_result to 0x0

fp = popen(cmd, "r");

while (fgets(cmd_result, sizeof(cmd_result), fp))
{
printf("%s", cmd_result);
}

pclose(fp);

But when I display cmd_result, it only shows the last line of the
command output.. .. any idea why?

Thanks,
Shaival

Barry Margolin

2005-01-03, 2:48 am

In article <1104180897.376255.286170@f14g2000cwb.googlegroups.com>,
"New User" <webcontacts00@yahoo.com> wrote:

> Hi,
>
> I tried this:
>
> FILE *fp;
> char cmd[10];
> zero(cmd); // metset cmd buffer to 0x0
> strcpy(cmd, "ls -al");
> char cmd_result[1000];
> zero(cmd_result); // memset cmd_result to 0x0
>
> fp = popen(cmd, "r");
>
> while (fgets(cmd_result, sizeof(cmd_result), fp))
> {
> printf("%s", cmd_result);
> }
>
> pclose(fp);
>
> But when I display cmd_result, it only shows the last line of the
> command output.. .. any idea why?
>
> Thanks,
> Shaival


Didn't you post this same code excerpt a few days ago? Did you repost
it for some reason, or is something resurrecting old posts automatically?

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com