Sponsored Content
Full Discussion: need help with execl command
Top Forums Shell Programming and Scripting need help with execl command Post 302070802 by Rakesh Ranjan on Friday 7th of April 2006 12:52:35 PM
Old 04-07-2006
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;
	 }

As the code is self-explanatory only few thing demand some clarification. The basic idea is that we use execute the different commands in different processes and output of one is fed to another using pipes and redirection (using pipe() and dup2(). U may use freopen() for redirection which is nothing but just the combination of these two only). Next the final output is redirected to a file (otherwise it will be displayed on terminal) which can then be read easily into some variable (here 'result'. Since I don't know how do u want to use this 'result' I've just displayed it back but u can do as u like).
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
 

10 More Discussions You Might Find Interesting

1. Programming

execl, execv or execp

Hi! I'm writing a C program which gets from the command line a shell command (such as "ls" ) and I should execute it. My Q is: how can I send a command to the shell? I know I have to use one of the above functions, but I don't know how to use them. Thanks eyal (1 Reply)
Discussion started by: azran
1 Replies

2. Programming

execl / execv ?

Hi, Is it possible to run a program from my C program using only the full pathname? for example if I wanna call: "ls", so I whould have to use: execl("/bin/ls", "ls", NULL); Is it possible to do this using only: "/bin/ls" thanks (1 Reply)
Discussion started by: owijust
1 Replies

3. Programming

How to specify path of a function within execl

Consider the following scenario program1: main() { ...... execl("path","function",...); ..... } function() { ----- ------- } Now i want to include the path of function in execl. How to do this. should the path be the path of function's executable file. If it so how... (1 Reply)
Discussion started by: bankpro
1 Replies

4. Programming

execl()

can anyone explain how to pass arguments of a program in execl function pls explain with a sample code. (2 Replies)
Discussion started by: bankpro
2 Replies

5. Programming

execl function at GDB

Hi, we would appreciate if any one answer the below query. void main() { printf(“ I am in main\n”); execl(“/HOME/source/file2”,” /HOME/source/file2”,1,0); printf(“after execl\n”); } How to step the file2 source code in GDB. (2 Replies)
Discussion started by: RAMESHPRABUDASS
2 Replies

6. Programming

time execl

hello everybody how can i time the execution of execl() command inside my C code? for example, i wrote.. execl("md5sum","md5sum","myprog",NULL); i want to count the duration of the execl command! thanx in advance! (2 Replies)
Discussion started by: nicos
2 Replies

7. Red Hat

execl command

how to use find command in execl function, I used: execl("/usr/bin/find","find","~","-name","filename.c",0); but it shows find: ~ no file and directory i need to get the path of the file from the home .:wall: (2 Replies)
Discussion started by: Mahendravarma
2 Replies

8. Programming

When execl fails in C

when execl fails using the command lss, it doesnt go into the next line execl("/bin/sh", "/bin/sh", "-c", command, NULL); perror("execl failed"); exit(127); for some reason the child process just stops and also the parent process also stops so the line after the line that... (3 Replies)
Discussion started by: omega666
3 Replies

9. Programming

[C] execl and pipes?

Hi, I have two programs, one is named "Master" and the other one "slave". What I want to do is , when I execute Master, inside slave will be called by excecl, do some calculations, and send those to the master program... A little example of what I am failing to do: if ((PID1=fork())==0) { //... (6 Replies)
Discussion started by: lamachejo
6 Replies

10. UNIX for Beginners Questions & Answers

Execl() command

Hi, If I write in a c file : execlp("date","date",NULL); printf("A\n"); And then run through the terminal would "A" be printed ? I understood that execlp will exit the program after it finished so the next lines of code won`t be executed afterwards.. Is that true ? (1 Reply)
Discussion started by: uniran
1 Replies
exec(1)                                                            User Commands                                                           exec(1)

NAME
exec, eval, source - shell built-in functions to execute other commands SYNOPSIS
sh exec [argument...] eval [argument...] csh exec command eval argument... source [-h] name ksh *exec [arg...] *eval [arg...] DESCRIPTION
sh The exec command specified by the arguments is executed in place of this shell without creating a new process. Input/output arguments may appear and, if no other arguments are given, cause the shell input/output to be modified. The arguments to the eval built-in are read as input to the shell and the resulting command(s) executed. csh exec executes command in place of the current shell, which terminates. eval reads its arguments as input to the shell and executes the resulting command(s). This is usually used to execute commands generated as the result of command or variable substitution. source reads commands from name. source commands may be nested, but if they are nested too deeply the shell may run out of file descrip- tors. An error in a sourced file at any level terminates all nested source commands. -h Place commands from the file name on the history list without executing them. ksh With the exec built-in, if arg is given, the command specified by the arguments is executed in place of this shell without creating a new process. Input/output arguments may appear and affect the current process. If no arguments are given the effect of this command is to mod- ify file descriptors as prescribed by the input/output redirection list. In this case, any file descriptor numbers greater than 2 that are opened with this mechanism are closed when invoking another program. The arguments to eval are read as input to the shell and the resulting command(s) executed. On this man page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a vari- able assignment. This means that tilde substitution is performed after the = sign and word splitting and file name generation are not performed. EXIT STATUS
For ksh: If command is not found, the exit status is 127. If command is found, but is not an executable utility, the exit status is 126. If a redi- rection error occurs, the shell exits with a value in the range 1-125. Otherwise, exec returns a zero exit status. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
csh(1), ksh(1), sh(1), attributes(5) SunOS 5.10 17 Jul 2002 exec(1)
All times are GMT -4. The time now is 11:51 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy