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 7804671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:59:29+00:00 2026-06-02T01:59:29+00:00

I am writing a small shell program that takes a command and executes it.

  • 0

I am writing a small shell program that takes a command and executes it. If the user enters a not valid command the if statement returns a -1. If the command is correct it executes the command, however once it executes the command the program ends. What am I doing wrong that is does not execute the lines of code after it? I have tested execvp( command.argv[0], command.argv) with ls and cat commands so I am pretty sure it works. Here is my code.

  int shell(char *cmd_str ){
  int commandLength=0;
  cmd_t command;
  commandLength=make_cmd(cmd_str,  command);
  cout<< commandLength<<endl;
  cout << command.argv[0]<< endl;
  if( execvp( command.argv[0], command.argv)==-1)
//if the command it executed nothing runs after this line
  {
    commandLength=-1;

}else
{
  cout<<"work"<<endl;
}

  cout<< commandLength<<endl;
   return commandLength;


}
  • 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-02T01:59:31+00:00Added an answer on June 2, 2026 at 1:59 am

    From man page of execvp(3)

    The exec() family of functions replaces the current process image with
    a new process image

    So your current process image is overwritten with the image of your command! Hence you need to use a fork+exec combination always so that your command executes in the child process and your current process continues safely as a parent!

    On a lighter note I want to illustrate the problem with a picture as a picture speaks a thousand words. No offence intended 🙂 🙂

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a c program for a class that is a small shell.
I'm writing a small shell script that needs to reverse the lines of a
Coming from a PHP background, I'm used to writing small functions that return a
I'm writing a small program to record reading progress, the data models are simple:
I've been writing some small maintenance/viewer tools that each connect to a SQLServer (2005)
I'm writing a small application in C that reads a simple text file and
I am currently writing an open source SDK for a program that I use
im writing a quiz program. if the user is wrong the 1st time, he
I'm currently in the process of building a small shell within C++. A user
I'm writing a small system that will allow me to sell my band's music

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.