LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-05-2003, 01:49 PM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
C program code to run a Linux command line


I would like a C program code to run a Linux command line such as ifconfig, testparm and others.

Below is a simple C program code, what command would I use so that this little program will execute the command ifconfig. If there is any syntax error below, please fix it also.

========================================
#include <stdio.h>

int main()

{
char *linux-command []
linux-command = "ifconfig"

# What function would I call so that linux-command would be
# execute as if the user in on a command line console.

}
 
Old 06-05-2003, 01:51 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Please do not post the same thread in more than one forum. Picking the most relevant forum and posting it once there makes it easier for other members to help you and keeps the discussion all in one place.

http://www.linuxquestions.org/rules.php
 
Old 06-05-2003, 02:02 PM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
system, defined in stdlib.h. check out man 3 system for definition
 
Old 06-05-2003, 04:19 PM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
please post on the forum and dont email me personally, thank you

i am not a linux programmer so this probably isnt perfect. do you know what pipes are? if not read about them. there is a function in stdio, popen that returns a FILE pointer but for a pipe instead of a file. read about popen then try the following

FILE *f;
f = popen("ls", "r");

// here you can use f as you would any normal FILE pointer but it
// contains the output of the ls command

pclose(f);

obviously you want to add some error checking in there and replace ls with ifconfig or whatever command you want to execute.
 
Old 06-07-2003, 03:10 AM   #5
Gopal
LQ Newbie
 
Registered: Jun 2003
Posts: 4

Rep: Reputation: 0
// You can use system() function

#include <stdio.h>
int main()
{
system("clear");
system("ifconfig");

}
 
Old 06-09-2003, 12:20 PM   #6
tcaptain
LQ Addict
 
Registered: Jul 2002
Location: Montreal
Distribution: Gentoo 2004 from stage 1 baby!
Posts: 1,403

Rep: Reputation: 45
I'm assuming that this would also apply to C++?

(I've been searching for info on this, but didn't know what to ask myself).
 
Old 06-10-2003, 05:33 PM   #7
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Yes, you can use stdio.h in C++ as well. Basically with a few rare incompatibilites, you can use anything in C in C++.
 
Old 06-10-2003, 08:39 PM   #8
tcaptain
LQ Addict
 
Registered: Jul 2002
Location: Montreal
Distribution: Gentoo 2004 from stage 1 baby!
Posts: 1,403

Rep: Reputation: 45
Quote:
Originally posted by Gopal
// You can use system() function

#include <stdio.h>
int main()
{
system("clear");
system("ifconfig");

}
When you use the system function like that? Where does the output go? (The man page doesn't mention...)
 
Old 06-10-2003, 11:02 PM   #9
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Well, my assumption is the output is displayed to the screen, like is the case with system("ls");

Now the more interesting question is, is there anyway to *capture* the output and use it directly in your program?

Obviously you could send the ouput of the command to a file and then read from the file. No, i am talking about whether or not you can do it without creating any files.

*Goes of hunting for answers*
 
Old 06-11-2003, 04:49 AM   #10
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
this is about the third time i have posted the same thing on this forum but nevermind. the output of a command can be captured by using popen() in stdio.h. its quite simple if you read the man pages.
 
Old 06-11-2003, 01:59 PM   #11
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Excellent, thanks.
For some reason, most of my "programming buddies" didn't know how to do it.

Last edited by coolman0stress; 06-11-2003 at 02:02 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Run Program From Command Line? neuropulse Linux - Newbie 11 05-28-2020 09:39 AM
C program code to execute linux-command supermichou_fr Linux - Software 4 08-01-2012 08:25 AM
How to program C in Linux command line backpacker Programming 4 06-24-2005 06:57 PM
Viewing program messages when program isn't run from command line? Locura Linux - Software 1 09-27-2003 08:19 AM
C program code calling a Linux system command Linh Programming 1 06-05-2003 01:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration