• Advertisement
Sign in to follow this  

using a linux shell command through c code

This topic is 4715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would I use a linux shell command through c code. For instance I want to zip/unzip, make directories, and see if files exist.

Share this post


Link to post
Share on other sites
Advertisement
corrington_j,

Use 'system' to do this. Its in the stdlib.h, and you can also read up more on it through the man page 'man system'

-brad

Share this post


Link to post
Share on other sites
Be aware that system() is seldom the best way to get things done. For example, there are standard C functions for making directories (mkdir), checking for file existence (stat/fstat/access), and executing commands (fork/exec).

Share this post


Link to post
Share on other sites
The system function should work on any OS, not just Linux.

Example:

#include <stdlib.h>

int main()
{
system("commandName");
return 0;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Dave Hunt
Be aware that system() is seldom the best way to get things done. For example, there are standard C functions for making directories (mkdir), checking for file existence (stat/fstat/access), and executing commands (fork/exec).

seconded. a bash shell is spawned inside your program, and if you want to parse the output you have to redirect to a file, which then needs to be opened...
and the dependencies (need the programs installed) which make it os dependent.

i don't mean to sound rude, but a shell script could do the job for what your doing.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ainokea
The system function should work on any OS, not just Linux.

Example:
*** Source Snippet Removed ***


And for completeness, in C++:

#include <cstdlib>

int main()
{
std::system( "command name and arguments" );
using std::system;
//or using namespace std;
system( "command name and arguments" );
}


(I'm aware the OP said C)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement