Go Back   The UNIX and Linux Forums > Top Forums > Programming
Search Forums:



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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-08-2007
Registered User
 

Join Date: Mar 2007
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy How to call Linux command in C

Dear Friends,
How we can call the Linux commands like ls, cat, grep, clear and others Linux commands in C programs.
Sponsored Links
    #2  
Old 04-08-2007
Technorati Master
 

Join Date: Mar 2005
Location: classification algos
Posts: 3,174
Thanks: 17
Thanked 26 Times in 24 Posts
system call
or popen command

am pretty sure many such threads have been posted in the forum

if you do search thats a definite 'HIT'

But still,


Code:
sprintf(cmd, "/bin/ls -l");
system(cmd);

Sponsored Links
    #3  
Old 04-09-2007
Registered User
 

Join Date: Mar 2007
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Linux command by C programm

Hi,
I think u can also call Linux commands using the exec family functions.
for that u need to read IPC programming in C.
    #4  
Old 08-15-2007
Registered User
 

Join Date: Aug 2007
Location: Manila, Philippines
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Hi, I'm calling the split command from inside C and i'm wondering why it is not working (the file is not being split). Also, when I try executing only the unix command in the shell, it's working just fine. Am I missing something here? Below is my code:
Quote:
char cmd[100];
sprintf(cmd, "split -l 50 -a 1 /absolutepath/sample.txt");
system(cmd);
Thanks in advance!
Sponsored Links
    #5  
Old 08-26-2007
Technorati Master
 

Join Date: Mar 2005
Location: classification algos
Posts: 3,174
Thanks: 17
Thanked 26 Times in 24 Posts
what is the return status of the system function call ( that uses split command ) ?
Sponsored Links
    #6  
Old 08-26-2007
Registered User
 

Join Date: Mar 2007
Location: Bangalore, India
Posts: 55
Thanks: 1
Thanked 1 Time in 1 Post
main()
{
system("split -l 50 -a 1 /absolutepath/sample.txt");
return 0;
}
Sponsored Links
    #7  
Old 09-14-2007
Registered User
 

Join Date: Sep 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by sriram003 View Post
main()
{
system("split -l 50 -a 1 /absolutepath/sample.txt");
return 0;
}
If you could provide the return-value of system, one could examine why the call is failing.

try:

Code:
#include <stdio.h>
#include <stdlib.h>
int main(const int argc, const char* const* const argv) {
 int res;
 int err;
 res = system("split -l 50 -a 1 /absolutepath/sample.txt");
 fprintf(stdout,"execution returned %d.\n",res);
 if ((-1 != res) && (127 != res))
  fprintf(stdout,"now would be a good time to check out 'man split' to check what the resulting return-value (%d) means.\n",res);
 return res;
}

Sponsored Links
Closed Thread

Tags
linux, linux commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
command << EOF(dont want to call other script) user_prady Shell Programming and Scripting 4 11-15-2007 03:19 AM
how to differentiate system call from library call muru UNIX for Advanced & Expert Users 2 07-19-2007 11:20 PM
How to call a Linux command in C prog. krishna_sicsr Shell Programming and Scripting 1 04-08-2007 03:53 AM
how can call perl script as command? umen UNIX for Dummies Questions & Answers 1 10-14-2005 09:43 PM
Making Socket System Call From Linux Kernel Module? mian_m_hamayun Linux 0 04-06-2005 09:34 AM



All times are GMT -4. The time now is 12:20 AM.