Skip to content

A Three-phase implementation of a custom Unix-like shell with local and remote execution capabilities.

License

Notifications You must be signed in to change notification settings

hinaamin375/custom-shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Custom Shell Implementation - C Programming

A two-phase implementation of a custom Unix-like shell with local and remote execution capabilities.

C Platform License

๐Ÿ“‹ Table of Contents

๐ŸŽฏ Overview

This project implements a custom command-line shell in C with two phases:

  1. Phase 1: A local shell that replicates standard Unix shell features
  2. Phase 2: A remote shell with client-server architecture using socket communication

The implementation demonstrates core systems programming concepts including process management, inter-process communication, file I/O, and network programming.

โœจ Features

Core Features

  • โœ… 15+ built-in commands (ls, cd, pwd, mkdir, rm, etc.)
  • โœ… External command execution (any system command)
  • โœ… Pipe support (single, double, and triple pipes)
  • โœ… File and directory operations
  • โœ… Error handling and validation

Phase 1 Features

  • Local command-line interface
  • Interactive shell prompt
  • Command parsing and execution
  • Process creation with fork/exec

Phase 2 Features

  • Client-server architecture
  • Remote command execution via TCP sockets
  • Output capture and network transmission
  • Multiple command support over network
  • Graceful connection handling

๐Ÿ“ Project Structure

custom-shell/
โ”œโ”€โ”€ phase1/
โ”‚   โ”œโ”€โ”€ phase1.c           # Local shell implementation
โ”‚   โ””โ”€โ”€ Makefile           # Build file for phase 1
โ”œโ”€โ”€ phase2/
โ”‚   โ”œโ”€โ”€ server.c           # Remote shell server
โ”‚   โ”œโ”€โ”€ client.c           # Remote shell client
โ”‚   โ”œโ”€โ”€ Makefile           # Build file for phase 2
โ”‚   โ””โ”€โ”€ test_phase2.sh     # Automated test script
โ”œโ”€โ”€ docs/
โ”‚   โ”œโ”€โ”€ PHASE1_GUIDE.md    # Phase 1 documentation
โ”‚   โ”œโ”€โ”€ PHASE2_GUIDE.md    # Phase 2 documentation
โ”‚   โ””โ”€โ”€ COMPARISON.md      # Phase 1 vs Phase 2 comparison
โ”œโ”€โ”€ README.md              # This file
โ””โ”€โ”€ LICENSE                # MIT License

๐Ÿ”ง Prerequisites

  • Operating System: Linux (Ubuntu, Debian, Fedora, etc.) or macOS
  • Compiler: GCC (GNU Compiler Collection)
  • Build Tools: Make
  • Development Headers: Standard C library development files

Installation on Ubuntu/Debian:

sudo apt-get update
sudo apt-get install build-essential gcc make

Installation on macOS:

xcode-select --install

๐Ÿ“ฆ Installation

  1. Clone the repository:
git clone https://github.com/yourusername/custom-shell.git
cd custom-shell
  1. Build Phase 1:
cd phase1
make
  1. Build Phase 2:
cd ../phase2
make

๐Ÿš€ Usage

Running Phase 1 (Local Shell)

cd phase1
./phase1

You'll see:

===========================================
  Custom Shell - Phase 1
  Type 'help' for available commands
  Type 'exit' to quit
===========================================

myshell>

Running Phase 2 (Remote Shell)

Terminal 1 - Start Server:

cd phase2
./server

Terminal 2 - Start Client:

cd phase2
./client

Or connect to a remote server:

./client 192.168.1.100

๐Ÿ“– Phase 1: Local Shell

Phase 1 implements a local command-line shell with the following capabilities:

  • Interactive command prompt
  • Built-in command execution
  • External program execution
  • Command pipelining (up to 3 pipes)
  • File and directory manipulation

Example Session:

myshell> pwd
/home/user/custom-shell

myshell> ls -l
-rw-r--r-- 1 user user  1234 Jan 01 12:00 file.txt
drwxr-xr-x 2 user user  4096 Jan 01 12:00 testdir

myshell> echo Hello World | wc -w
2

myshell> exit
Exiting shell...

See docs/PHASE1_GUIDE.md for detailed documentation.

๐ŸŒ Phase 2: Remote Shell

Phase 2 extends Phase 1 with network capabilities using a client-server architecture:

  • Server: Executes commands and captures output
  • Client: Sends commands and displays results
  • Communication: TCP sockets on port 8080
  • Protocol: Simple text-based message passing

Architecture:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”         Network          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Client  โ”‚ โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ โ”‚ Server  โ”‚
โ”‚ (I/O)   โ”‚    Commands/Results      โ”‚ (Exec)  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                           โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

See docs/PHASE2_GUIDE.md for detailed documentation.

๐Ÿ“ Supported Commands

Built-in Commands

Command Description Example
cd Change directory cd /home/user
pwd Print working directory pwd
echo Print text echo Hello World
ls List directory contents ls -l
mkdir Create directory mkdir newdir
rmdir Remove empty directory rmdir olddir
rm Remove file/directory rm file.txt or rm -r dir
touch Create empty file touch newfile.txt
cat Display file contents cat file.txt
clear Clear screen clear
help Show help message help
exit Exit shell exit

External Commands

Any system command available on your PATH:

  • whoami, hostname, date, uname
  • grep, awk, sed, sort
  • wc, head, tail, cut
  • And many more...

Pipe Support

# Single pipe
command1 | command2

# Double pipe
command1 | command2 | command3

# Triple pipe
command1 | command2 | command3 | command4

๐Ÿ’ก Examples

Basic Commands

# Navigate and list
myshell> cd /tmp
myshell> pwd
/tmp
myshell> ls -l

# File operations
myshell> touch test.txt
myshell> echo "Hello World" > test.txt
myshell> cat test.txt
Hello World

Piped Commands

# Count files in directory
myshell> ls | wc -l

# Search in file
myshell> cat /etc/passwd | grep root

# Complex pipeline
myshell> ps aux | grep bash | awk '{print $2}' | wc -l

Remote Execution (Phase 2)

# On client
remote-shell> ls /home
remote-shell> cat /etc/hostname
remote-shell> df -h | grep /dev

๐Ÿ”ฌ Technical Details

Key Technologies Used

  1. Process Management

    • fork() - Create child processes
    • execvp() - Execute programs
    • waitpid() - Wait for child completion
  2. Inter-Process Communication

    • pipe() - Create pipes for data flow
    • dup2() - Duplicate file descriptors
    • File descriptor manipulation
  3. Network Programming (Phase 2)

    • socket() - Create network endpoints
    • bind(), listen(), accept() - Server setup
    • connect() - Client connection
    • send(), recv() - Data transmission
  4. File Operations

    • opendir(), readdir() - Directory traversal
    • stat() - File information
    • mkdir(), rmdir(), unlink() - File system operations

Design Patterns

  • Command Pattern: Each command is encapsulated in its own function
  • Chain of Responsibility: Pipeline processing with multiple commands
  • Client-Server Pattern: Phase 2 architecture
  • Facade Pattern: Simple interface over complex system calls

๐Ÿงช Testing

Automated Testing (Phase 2)

cd phase2
chmod +x test_phase2.sh
./test_phase2.sh

Manual Testing

# Test basic commands
ls
pwd
echo test

# Test pipes
ls | grep .c
cat file.txt | wc -l
ps aux | grep bash | wc -l

# Test file operations
mkdir testdir
cd testdir
touch file.txt
cat file.txt
cd ..
rm -r testdir

Test Coverage

  • โœ… Basic built-in commands
  • โœ… File and directory operations
  • โœ… Single pipe commands
  • โœ… Double pipe commands
  • โœ… Triple pipe commands
  • โœ… External command execution
  • โœ… Error handling
  • โœ… Network communication (Phase 2)

๐Ÿค Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Development Guidelines

  • Follow existing code style
  • Add comments for complex logic
  • Test your changes thoroughly
  • Update documentation as needed

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ‘ฅ Authors

๐Ÿ™ Acknowledgments

  • Inspired by Unix/Linux shell implementations
  • Built as part of Operating Systems coursework
  • Thanks to the open-source community

๐Ÿ“š Additional Resources

๐Ÿ› Known Issues

  • Phase 2 server handles one client at a time (no concurrent connections)
  • No authentication mechanism in Phase 2
  • Limited to 3-level pipe depth
  • No command history support

๐Ÿ”ฎ Future Enhancements

  • Multi-client support with threading
  • Command history and auto-completion
  • Configuration file support
  • Authentication and encryption (SSL/TLS)
  • Job control (background processes)
  • Signal handling improvements
  • Environment variable support
  • Redirection operators (>, >>, <)

Note: This is an educational project. For production use, consider security implications and proper error handling.

About

A Three-phase implementation of a custom Unix-like shell with local and remote execution capabilities.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published