How to call Linux command in C


 
Thread Tools Search this Thread
Top Forums Programming How to call Linux command in C
# 1  
Old 04-08-2007
Data 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.
# 2  
Old 04-08-2007
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' Smilie

But still,

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

# 3  
Old 04-09-2007
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
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!
# 5  
Old 08-26-2007
what is the return status of the system function call ( that uses split command ) ?
# 6  
Old 08-26-2007
main()
{
system("split -l 50 -a 1 /absolutepath/sample.txt");
return 0;
}
# 7  
Old 09-14-2007
Quote:
Originally Posted by sriram003
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;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Ubuntu

Help: how to call fork() in shell script? New to linux

Hi, I'm writing a shell script where I want to call fork(). However I wrote like this "var=fork()" in c style and got this error: "syntax error near unexpected token `(' " How could I call fork() in shell script? Thanks in advance. (2 Replies)
Discussion started by: Xiaoya
2 Replies

2. Shell Programming and Scripting

Help: how to call fork() in shell script? New to linux

Hi, I'm writing a shell script where I want to call fork(). However I wrote like this "var=fork()" in c style and got this error: "syntax error near unexpected token `(' " How could I call fork() in shell script? Thanks in advance. Duplicate Post - Continue Here - Please Do Not Cross Post... (0 Replies)
Discussion started by: Xiaoya
0 Replies

3. Programming

can a linux kernel module call libc functions?

can a linux kernel module call libc functions, such as printf(), strcpy(), etc...? (9 Replies)
Discussion started by: vistastar
9 Replies

4. Shell Programming and Scripting

Perl - Call a sub routine in a command

Hi all I have written a simple perl script that has different options i.e. myscript -l -p etc i have it so when it runs without any switches it runs a subroutine called nvrm_norm i want to be able to do a -p option and run pall -w -f and then called the subruotine pall is... (1 Reply)
Discussion started by: ab52
1 Replies

5. Windows & DOS: Issues & Discussions

How to call a command in a batch file?

Hi, I would like to get the output of below command emailed to me in a windows2003 server. "bpimagelist -hoursago 24 -U" I will be using "blat" to email the output of this command.But not sure how the above command is called for in a batch file when executed. Would appreciate if... (1 Reply)
Discussion started by: Hari_Ganesh
1 Replies

6. Linux

How to implement a system call in linux?

I encountered a variety of difficulties when implementing a system call as simple as HelloWorld example in Linux so far. Firstly, according to the book "Linux Kernel Development Second Edition", I tried to implement a syscall in 2.6.31 but no way absolutely because 2.6.10 has a very different way... (5 Replies)
Discussion started by: sunnyhay
5 Replies

7. UNIX for Advanced & Expert Users

ioctl() system call on Linux-i386

Greetings, Please help me with the following : Where can I find what means exactly and how to use each of the second argument of the ioctl() system call in Linux/386 : FIOxxx (file IOCTL requests), SIOxxx (socket IOCTL requests), TCxxx TIOxxx (terminal IOCTL requests) ? ... (1 Reply)
Discussion started by: aigoia
1 Replies

8. Shell Programming and Scripting

How to call a Linux command in C prog.

Dear Friends, I want to know how to call a Linux commands in C programs. (1 Reply)
Discussion started by: krishna_sicsr
1 Replies

9. UNIX for Dummies Questions & Answers

how can call perl script as command?

Hello say i have written some perl scripts , now i like to call them in my unix shell as unix command like "more" , "ls" , "grep" so that my say perl script called "foo.pl" will be called from every where as "foo" or "foo arg1 arg2"? Thanks (1 Reply)
Discussion started by: umen
1 Replies

10. Linux

Making Socket System Call From Linux Kernel Module?

Hi Everyone! How can we make a socket() system call from a linux module executing in kernel space? If any one knows, kindly tell me. It will be great. I want to use the socket interface in linux kernel space for sending raw packets over the network. Hamayun (0 Replies)
Discussion started by: mian_m_hamayun
0 Replies
Login or Register to Ask a Question