Capture unix command shell output to a buffer
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix questions > Capture unix command shell output to a buffer




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Capture unix command shell output to a buffer  
New User


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-22-04 10: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.






[ Post a follow-up to this message ]



    Re: Capture unix command shell output to a buffer  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-23-04 01:54 AM

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 ***





[ Post a follow-up to this message ]



    Re: Capture unix command shell output to a buffer  
New User


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-27-04 10: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,






[ Post a follow-up to this message ]



    Re: Capture unix command shell output to a buffer  
Arto Viitanen


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-28-04 07: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/





[ Post a follow-up to this message ]



    Re: Capture unix command shell output to a buffer  
New User


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-03-05 07: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






[ Post a follow-up to this message ]



    Re: Capture unix command shell output to a buffer  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-03-05 07: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 ***





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:09 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register