how could i write this program: cat < apa | wc | wc > bepa

This is a discussion on how could i write this program: cat < apa | wc | wc > bepa within the C Programming forums, part of the General Programming Boards category; hi, i am a new programmer in linux programming, i want to write a program that demoes the function of ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    4

    how could i write this program: cat < apa | wc | wc > bepa

    hi,

    i am a new programmer in linux programming, i want to write a program that demoes the function of shell, i just write some simple functions, for example, i have written
    ls | wc -w
    wc -l < somefile > anotherfile

    but they could just run separately, if i need to write more complex, such as:
    ls | wc -w | sort
    i do not know how to do that, the most problem is that i do not know what order should i make in the program, if there is only one "|", i could use the pipe to read what the first part runs then make the result as input of the second part, but now there are two "|", i do not know how to do it

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    4
    i could use execlp("ls","ls"NULL) to get the result and it will output to the standard output, which is now redirected to one end of pipe, the other end is used to give input for wc -w, but if there are two or more "|", how could i finish this function

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    4
    I use the following code to do the pipe program such as: ls | wc -w for only one "|", but if there are more "|", i do not know how to deal with it

    Code:
    void pipeProcess(char *args1[], char *args2[]) {
        int thepipe[2], pid;
    
        if(pipe(thepipe) == -1)
            oops("Can not get a pipe", 1);
        /* Create a new process */
        if((pid = fork()) == -1)
            oops("Can not fork", 2);
    
        if(pid >0) {/* This is parent process */
            close(thepipe[1]);      /* parent does not write to pipe */
            if(dup2(thepipe[0], 0) == -1)   /* let parent read interface attaches to stdin */
                oops("Could not redirect stdin", 3);
            close(thepipe[0]);      /* close thepipe[0] interface because it has attached to stdin */
            execvp(args2[0], args2);
            oops(*args2, 4);
        }
    
        /* Child process executes av[1] and writesinto pipe */
        close(thepipe[0]);      /* Child does not read from pipe */
        if(dup2(thepipe[1], 1) == -1)
            oops("Could not redirect stdout", 4);
        close(thepipe[1]);
        execvp(args1[0], args1);
        oops(*args1, 5);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. How to write a program
    By Tashfique in forum C++ Programming
    Replies: 4
    Last Post: 10-17-2008, 11:28 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21