pipes and shells


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers pipes and shells
# 1  
Old 10-13-2008
pipes and shells

Hi
How do I direct the output from ls to for example grep a in C. I am not asking for the whole shell implementation.

If I write ls|grep myfile in the shell. How is the output sent from ls to the input grep. whit int pipe(pipe[2]); We create the pipe. I and I guess we use dup2(old filedescriptor,new filedescriptor);, What should I do whit pipe[0](readend), and the pipe[1](writeend)?

Regards
# 2  
Old 10-13-2008
An example how to get the result of the ls|grep command:

Code:
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
  FILE *pipein;
  char readbuf[80];

  if ((pipein = popen("ls | grep <myfile>", "r")) == NULL)
  {
    perror("popen");
    exit(1);
  }

  fgets(readbuf, 80, pipein);
  printf("%s", readbuf);

  close(pipein);
  return(0);
}

Regards
# 3  
Old 10-13-2008
Thank you very much for your reply. But if I do it whit fork and pipes?
# 4  
Old 10-14-2008
You should read a tutorial regarding IPC programming, a useful link:

Guide to Unix IPC

Regards
# 5  
Old 10-15-2008
Whats wrong whit this pipe?

Could anyone tell me whats wrong whit this piping? the commands that they execute are correct. the command I am trying is ls|wc. Both processes go to the right if statement.


Code:
for(i=0;i<argc;i++){
   
       if(i==0&&argc>1){//first command
           if(pipe(pipa1)==-1)
               perror("pipe");
           if((pid=fork())<0)    
                perror("fork");
            if(pid==0){//child
                printf("jag är fösta kommandot");
                printf("I am a 1 process--%d, my parent is--%d jag skall exikvera--%s\n ",getpid(),getppid(),com[i].argv[0]);
                dupPipe(pipa1, WRITE_END, STDOUT_FILENO);
                execvp(com[i].argv[0],com[i].argv);
            }                
       }
       if (i>0 && argc==2) {       
         pid = fork();          
         if (pid == 0) {        
            printf("jag är andra kommandot");
            printf("I am a process--%d, my parent is--%d jag skall exikvera--%s \n ",getpid(),getppid(),com[i].argv[0]);
            dupPipe(pipa1, READ_END, STDIN_FILENO);
            execvp(com[i].argv[0],com[i].argv);
             }
   
        }


Code:
/* Duplicate a pipe to a standard I/O file descriptor, and close both pipe ends
 * Arguments:    pip    the pipe
 *        end    tells which end of the pipe shold be dup'ed; it can be
 *            one of READ_END or WRITE_END
 *        destfd    the standard I/O file descriptor to be replaced
 * Returns:    -1 on error, else destfd
 */
int dupPipe(int pip[2], int end, int destfd){
 
  if (-1 == dup2 (pip[end], destfd))
    {
      perror ("dupPipe()");
      return -1;
    }
  return destfd;
}

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Please what are shells?

I mean like this: http://shells.red-pill.eu/ Can anyone explain how this works? I hope my post is not spam. I think its related to linux. Thank you (1 Reply)
Discussion started by: postcd
1 Replies

2. UNIX for Dummies Questions & Answers

Shells

Lets say my default shell is bash and then i load up csh and then ksh. How would i exit csh without exiting ksh? so basically i gone from bash > csh > ksh and i wish to close csh (2 Replies)
Discussion started by: Bill Thompson
2 Replies

3. Programming

Pipes in C

Hello all, I am trying to learn more about programming Unix pipes in C. I have created a pipe that does od -bc < myfile | head Now, I am trying to create od -bc < myfile | head | wc Here is my code, and I know I might be off, thats why I am here so I can get some clarification. #include... (1 Reply)
Discussion started by: petrca
1 Replies

4. Linux

/etc/shells on /etc/passwd

Hi All, Why does these list (below) doesn't belong to /etc/shells? sync:x:5:0:********************// :/sbin:/bin/sync shutdown:x:6:0:********************// :/sbin:/sbin/shutdown halt:x:7:0:********************// :/sbin:/sbin/halt webalizer:x:68:68:***************// ... (2 Replies)
Discussion started by: itik
2 Replies

5. UNIX for Advanced & Expert Users

why we have different shells?

Can you pls. tell me, why we have different shells in UNIX OS ( Eg. SunOs) and also I would like to know what is the specific difference b/w SVR and BSD ? Thanks. (2 Replies)
Discussion started by: shahnazurs
2 Replies

6. Linux

Version of Shells?

Can anyone tell me the version of shells for bash mc pdksh tcsh zsh For Red Hat Enterprise 4? Thank you very much! (2 Replies)
Discussion started by: Xplore
2 Replies

7. UNIX for Advanced & Expert Users

Shells

I have came across the definitions of these shells korn bourne c etc .. but honestly till now i din't get the exact difference between these threes , the advantages ..... can anyone pinpoint me where it actually lies ..... don;t include me answers like aliasing in c is posible and not in bourne ..... (3 Replies)
Discussion started by: dino_leix
3 Replies

8. UNIX for Dummies Questions & Answers

Restricted Shells. . .

Hey, Could someone please help me distinguish between a captive account and a restricted shell? Many thanks, L. (0 Replies)
Discussion started by: crispy
0 Replies

9. Shell Programming and Scripting

cd using pipes

Hi, Can the cd command be invoked using pipes??? My actual question is slightly different. I am trying to run an executable from different folders and the path of these folders are obtained dynamically from the front end. Is there a way in which i can actually run the executable... (2 Replies)
Discussion started by: Sinbad
2 Replies

10. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question