Skip to content

About

Unix shell in C: pipelines, redirection, background jobs, signal handling

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

12 Commits

Folders and files

Repository files navigation

MiniShell

MiniShell is a simple Unix shell implemented in C. This project is designed to help you understand operating system concepts such as process management, signal handling, and command execution. It also demonstrates how to implement features like pipelines, redirection, and background processing.

Features

  • REPL Loop (Read-Evaluate-Print Loop):
    Continuously reads user input, evaluates commands, and prints the output with a custom prompt.

  • Advanced Parsing:

    • Tokenizes input by spaces.
    • Supports quoted strings (both single and double quotes) and escape sequences.
  • Built-in Commands:

    • cd: Changes the current working directory.
    • exit: Exits the shell.
  • External Command Execution:
    Uses fork() and execvp() to execute external commands.

  • Pipeline Support:
    Supports the | operator to chain commands—allowing the output of one command to become the input of the next.
    Example:

    ls -l | grep ".c"
    
  • Output Redirection:
    Supports the > operator to redirect the output of a command to a file.
    Example:

    cat > output.txt
    
  • Background Processing:
    Appending & to a command runs it in the background.

  • Signal Handling:

    • SIGINT: Captures Ctrl+C to prevent the shell from terminating unexpectedly.
    • SIGCHLD: Cleans up terminated background processes to avoid zombie processes.

Project Structure

.
├── README.md
├── Makefile
├── main.c
├── parser.c
├── parser.h
├── builtins.c
├── builtins.h
├── executor.c
├── executor.h
├── signals.c
└── signals.h

Compilation

To compile the project, open a terminal in the project directory and run:

make

This command uses gcc with appropriate flags to compile all the source files and produces an executable named minishell.

Usage

Start the shell by running:

./minishell

Once started, you will see a prompt like myshell>. You can now enter commands as you would in a regular Unix shell.

Examples

  • Simple Command Execution:

    myshell> ls -l
  • Pipeline Execution:

    Chain commands using the pipe operator:

    myshell> ls -l | grep ".c"

    In this example, the output of ls -l is passed directly as input to grep, filtering the results to show only files with a .c extension.

  • Output Redirection:

    Redirect command output to a file:

    myshell> cat > output.txt

    After running the command, type your input and press CTRL+D to save it to output.txt.

  • Background Processing:

    Run a command in the background:

    myshell> sleep 10 &

    This will execute sleep 10 in the background and immediately return the prompt.

Test Scenarios

You can verify the functionality of MiniShell by testing:

  1. Pipeline Test:
    Run:

    ls -l | grep ".c"
    

    Verify that only files with a .c extension are displayed.

  2. Output Redirection Test:
    Run:

    cat > output.txt
    

    Type some input, press CTRL+D to finish, then check the file content with:

    cat output.txt
    
  3. Background Processing Test:
    Run:

    sleep 10 &
    

    Ensure that the shell immediately returns a prompt and displays the background process PID.

Future Enhancements

  • Input Redirection: Add support for the < operator to redirect input from a file.
  • Append Redirection: Implement the >> operator to append output to an existing file.
  • Remote Command Execution: Use socket programming to allow remote command execution.
  • Additional Built-in Commands & Command History: Expand built-in functionalities and add a history feature.
  • Improved Error Handling & Logging: Enhance error detection, reporting, and logging mechanisms.

License

This project is developed for educational purposes. You are free to use, modify, and distribute it as needed.

Acknowledgements

Thanks to various online resources and documentation on Unix system programming and shell implementation, which have been invaluable in developing this project.

About

Unix shell in C: pipelines, redirection, background jobs, signal handling

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages