![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| call execl with int as argument | Dana73 | High Level Programming | 2 | 02-10-2006 02:42 PM |
| execl() | bankpro | High Level Programming | 2 | 02-01-2006 09:48 AM |
| How to specify path of a function within execl | bankpro | High Level Programming | 1 | 01-24-2006 03:00 AM |
| execl / execv ? | owijust | High Level Programming | 1 | 01-23-2006 01:39 AM |
| execl, execv or execp | azran | High Level Programming | 1 | 04-22-2002 09:58 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
need help with execl command
I want to make simultanous sh commands in an exec command
for example I want to counts the lines in a file wc -l my file.txt | awk -F" " '{print $1}'` works fine in sh but I want to implement it in a c code the first part works like this execl("/usr/bin/wc", "wc", "-l", "myfile.txt", (char *)0); for the second part it will be sth like this I think execl("/bin/awk", "awk", "-F", "'{print $1}'", (char *)0); I want to implement this two command together like in the sh commands I want to bound to first exec command's output to second exec command's output like as what the " | " operatoretor does in shell. and also I want to assign final output to a variable in c I know this is a little bit long but It will be greatfull if anyone can help me, Thanks soo much.. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Since it bit too late that someone is answering I would just like to ask are u still in search of ur answer if yes I'm listing code for what u desired below:
Code:
/* listing - cpipe.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define CLOSEALL() close(pipefd[0]),close(pipefd[1])
int main(int argc,char *argv[]){
int pipefd[2],fd;
pid_t pid1,pid2;
int result;
if (argc < 2){
printf("Argument Missing\nUsage: ./cpipe filename\n");
exit(1);
}
if (pipe(pipefd) < 0){
perror("pipe:");
exit(1);
}
pid1 = fork();
if(pid1==-1){
perror("fork");
exit(1);
}
else if(pid1==0){
pid2 = fork();
if(pid2 == -1){
perror("fork");
exit(1);
}
else if(pid2 == 0){
dup2(pipefd[1],STDOUT_FILENO);
CLOSEALL();
if(execlp("/usr/bin/wc","wc","-l",argv[1],0)< 0){
perror("execlp");
exit(1);
}
}
else{
dup2(pipefd[0],STDIN_FILENO);
if((fd = open("temp",O_RDWR|O_CREAT|O_TRUNC,0666))==-1){
perror("open");
exit(1);
}
dup2(fd,STDOUT_FILENO);
CLOSEALL();
close(fd);
if(execlp("/usr/bin/awk","awk","-F"," ","{print $1}",0) < 0){
perror("execlp");
exit(1);
}
}
}
else{
CLOSEALL();
waitpid(pid1,NULL,0);
FILE *fp = fopen("temp","r");
fscanf(fp,"%d",&result);
fclose(fp);
//unlink("temp");
printf("And the result is :: %d\n",result);
}
return 0;
}
Also u may want to delete the file that stores ur output temporarily (here 'temp'). U can call unlink() to do so (Though I've commented it out for this would require file to be created every time u run the prog. an unneccessary operation but if its necessary to keep data secret or for any other reason u may either name the file '.temp' to hide it or uncomment the unlink()). If any more elaboration is required let me know. I'll try to dispell ur doubts. U can also get some refernces for it in Unix System Programming by Terrance Chan or Advanced Unix Programming by Stevens |
||||
| Google The UNIX and Linux Forums |