Create a new process and execute the command in new process

Problem Statement

You will be writing a program that presents its user with a shell in which they can type commands and see their results. This shell should support changing directories, running commands, redirecting input and/or output, and piping output from one command to the input of another command.

Problem Details

You are to take the provided Command class (which can read commands from the keyboard) and use it to repeatedly read in and process commands until the user types in "exit" as a command.

With the exception of the "cd" and "exit" commands, each command should create a new process and execute the command in the new process. Some other details:

  1. The "exit" command should cause your shell program to end. printing a message indicating such.
  2. The "cd" command should used the chdir() system call to change directories in the current shell. The chdir function call takes one parameter, namely a string (c-style character array/ pointer version) representing the name of the directory to change into.
  3. Other commands should be executed using an execvp() call.
  4. A command with an input redirect should use the contents of the specified input file as if it is standard input. For example,
    wc -l < data.txt
    Will count the number of lines (the wc - l command) found in the file data.txt and display that number on standard output (i.e. on the console.)
  5. a command with an output redirect should use the contents of the specified output file as if it is standard output. For example,
    wc -l < data.txt > output.txt
    will count the number of lines (the wc - l command) found in the file data.txt and output the result in the file output.txt . Note that this will overwrite the contents of the file output.txt. Also note that you can do file output redirection without file input redirection.
  6. A command specified to run in the background (by adding an &) means that the shell should immediately allow another command to be entered at a new command prompt. When a backgrounded program completes, a summary including the name of the command and its PID should be printed.
  7. On the other hand, a command entered without an & indicating background processing should wait for the associated command to complete before prompting the user to enter another command.
  8. A command with a pipe out ("|") should redirect its output to a pipe. This pipe should then be used as the input for the next command read. For example,

cat somefile.txt | sort

should output the file somefile.txt (i.e. cat somefile.txt) into a pipe (not on the screen) and then another command (i.e. sort) uses the pipe for its input (and displays its output on the screen, since there is no pipe out for the sort command or any output file redirection for the sort command.)

Remember that upon exit your shell program should print a message indicating that your shell has completed.

WhatsApp icon