Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8030787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:52:56+00:00 2026-06-05T00:52:56+00:00

Trying to write a shell that processes internal and external commands. I can get

  • 0

Trying to write a shell that processes internal and external commands. I can get the internal commands and one external at a time.

My question is how to get a command like this to run: “ls -l | grep lib | wc -l”

I’m using a fork(), and passing external commands through execv() in a char*[].

Any thoughts on how to work this? I assume using pipe() or something, but I’m not sure.

Second part of the question: how about dealing with i/o redirection? Can anyone point me to somewhere helpful?

EDIT
So far, @Alex W is my hero. However, since I’m new to the pipe() and dup2() commands, I’m a little hesitant on what each call and variable are for.

Here’s the code I have that handles a single external command (Example = “ls -l -a”):

    pid_t pid;
    pid = fork();
    if (pid < 0)
    {
        cout << "Fork failed." << endl;
    }
    else if (pid == 0)
    {
        execvp(exec_args[0], exec_args);   //exec_args is a char*[] where 
        _exit (EXIT_FAILURE);              //exec_args[0] contains "/bin/ls"
    }                                          //[1]="ls" and [2]="-l" [3]="-a"
    else
    {
        int status;
        waitpid(pid, &status, 0);
    }
    break;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:52:57+00:00Added an answer on June 5, 2026 at 12:52 am

    You do use pipe and in POSIX systems a pipe works in a similar manner to a file. You can write to a pipe and read from a pipe, but if there is nothing in the pipe it blocks. The best place to find information on Unix system calls is the man page for the system call. On a Unix system you would type man pipe into a terminal. If you don’t have access to a Unix terminal then just google “man pipe”. The nice thing about the man pages is they tell you what libraries to include for the given system call. Make sure you remember that when using any exec type system call you are loading a completely new process into that memory and the process that you were executing will stop dead in its tracks.

    To use it do something like this:

    int main()
    {
        int id[2];
        int fd[2];
        int fd2[2];
        int fd3[2];
        FILE file;
        int status;
        int sz = 0;
        char buff[1000];
        char buff2[1000];
        string launch[2];
        FILE *fp;
    
        launch[0] = "./anotherProgramToExecute";
        launch[1] = "./yetAnotherProgram";
    
        pipe(fd);
        pipe(fd2);
        pipe(fd3);
    
        for(int i = 0; i < 2; i++)
        {
            id[i] = fork();
    
            if (id[i] == -1) /* an error occurred */
            {
                perror("Fork failed.\n");
            }
            else if (id[i] == 0) /* this is the child process currently acting */
            {
                if(i == 0)
                {
                    dup2(fd[1],1);
                }
                else if(i == 1)
                {
                    dup2(fd2[0],0);
                    dup2(fd3[1],1);
                }
                execlp(launch[i],launch[i], NULL);
            }
            else /* this is the parent process currently acting */
            {
                sz = read(fd[0], buff, 1000);
                buff[sz] = '\0';
                printf("buff = %s\n",buff);
    
                close(fd[0]);
    
                write(fd2[1],buff, 1000);
    
                read(fd3[0],buff2,1000);
    
                fp = fopen("bin.txt","w");
                if(fp == NULL)
                    printf("Cannot open file.\n");
                else
                {
                    fprintf(fp,buff2);
                    fclose(fp);
                }
    
                //printf("Creation of Child Process #%d succeeded!\n",id[i]);
                while(waitpid(id[i], &status, WNOHANG) == 0)
                    sleep(0.3);
                if (WIFEXITED(status))
                { 
                    // successfully terminated children
                }
                else
                {
                    perror("Child has not terminated correctly.\n");
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a wrapper shell script that caches information every time
I am trying to write an awk script that can get a block that
Im trying to write a program that runs some shell commands wit emulated user
I am trying to write to the /dev/ttyACM0 port. In the command line shell
Trying to write a chat, like on facebook, I wondered if two clients can
I'm trying to write a shell script that will make several targets into several
I am trying to write a simple shell-script that prints out the first parameter
I'm trying to write a shell script that does a search and replace inside
hi I am trying to write a bourne-shell script that takes a name as
I am trying to write a bash shell script that consumes a high amount

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.