The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem in running bash shell commands on HP-UX machine abhishek0071 UNIX for Advanced & Expert Users 6 02-15-2008 03:30 PM
Running shell scripts automatically without using Batch or at commands ritzwan0 Shell Programming and Scripting 3 09-17-2006 02:51 PM
How to disable running commands from vi Umesh_Sharoff Shell Programming and Scripting 1 07-14-2006 12:05 PM
running commands from script owijust Shell Programming and Scripting 2 01-09-2006 06:12 PM
running start up commands skotapal UNIX for Dummies Questions & Answers 3 12-03-2002 07:31 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 3.00 average. Display Modes
  #1 (permalink)  
Old 08-19-2008
alirezan alirezan is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 22
Running shell commands from C/C++

Hi guys,

I know using system() we can run unix commands but the problem is, I can't get any returns with the system(). I am returning stuff from my shell scripts that I need to be able to read from my C code.

Anybody has cure to this problem? )

Thanks
  #2 (permalink)  
Old 08-19-2008
kpedersen kpedersen is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 12
Hello,

I remember doing this some time ago,

If you want to capture the output, consider popen() if your C library supports it.

(v)fork(), exec() might be worth a look too

Sorry I couldn't be more help, I had a dig around for the old code (SDL Battery Status App) but I couldn't find it
  #3 (permalink)  
Old 08-20-2008
iamcollins iamcollins is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 8
open should do this

Hello

popen function should do this. You can execute a command using popen function in either read or write mode and the result will be return as a file pointer. You can then read the result from the command using the file pointer as you normally do..

A simple example i got from googling is below
#include <stdio.h>

int main() {
FILE *in;
extern FILE *popen();
char buff[512];

/* popen creates a pipe so we can read the output
of the program we are invoking */
if (!(in = popen("netstat -n", "r"))) {
exit(1);
}

/* read the output of netstat, one line at a time */
while (fgets(buff, sizeof(buff), in) != NULL ) {
printf("Output: %s", buff);
}

/* close the pipe */
pclose(in);
}

Hope this should help you

Regards
Collins
Closed Thread

Bookmarks

Tags
unix commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:27 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0