Skip to content

Minishell is a small-scale implementation of a Unix-like shell🐚📚✨.

Notifications You must be signed in to change notification settings

belkarto/minishell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub code size in bytes Number of lines of code Code language count GitHub top language GitHub last commit

Minishell

Description

This project is a simple shell written in C that provides a basic command-line interface.

History

story behind shell
In its earliest form, a terminal was a small machine consisting of a monitor and a keyboard that allowed people to communicate with a larger computer system.
The terminal continued to improve and eventually led to the creation of the Bash shell. Bash is now one of the most widely used shells on Unix-based operating systems and is the default shell on most Linux distributions.

So how can we create a small version of if using C programming language ?

Installation

1 - Clone this repository:

git clone https://github.com/belkarto/minishell.git

2 - Add readline path to Makefile:

   NOTE :

   If you do not have readline, you can install it by using:

brew install readline

Features

  • Command Parsing
  • Command Search && Execution
  • Input/Output Redirection
  • Pipe Execution
  • Variable Expansion

Lexer && Parsing

--> Lexer

The lexer takes the command line and parses it into tokens. Therefore, there are two types of tokens:
- Word
- Special characters (“, ‘, <, >, <<, >>, $, |, spc)

Click for Example :
Click for Structs :

--> Parser

The parser takes the sequences of tokens generated by the lexer, allocates a command table, puts the content of the command in the cmd_tab.cmd and saves the files in cmd_tab.redir while checking the syntax error.
The lexer and the parser work together to transform the command line into a form that can be executed.


Note: You can use a command table if your shell is handling only pipes, else in case of handling logical operators like "&&" or "||" the best way is to use a data structure called Abstract Syntax Tree (AST).

Click for Examples :
Click for Struct :

Execution

Simple command execution steps example:
	    						   +------------+	
							|--| Open all   |----|
	                                    		|  | heredocs   |    |
							|  +------------+    |
+-------------------+     +--------------------------+	|		     |     +-------------------------+   +---------------------+  
|    Take cmd_tab   |-----|  Check for redirections  |--|		     |-----| Change stdin and stdout |---| Execute the command |
+---------+---------+     +------------+-------------+	|		     |     +-------------------------+   +---------------------+
	    						|  +------------+    |
	    						|--|open other 	|----|
	    						   |redirictions|
							   +------------+
								


Command search

Before executing a command we need to look it up first we first check it if its executable by using access() in case of error we search the command in the directories specified in the system's PATH environment variable. If the command is found, the program moves on to the next step. If it's not found, you'll get a "command not found" error

Command execute

After finding the command path, the program executes it, launching a new process to run the command.

communication between command "|"

You can use the "|" character to pipe the output of one command into the input of another command. This allows you to chain multiple commands together to perform more complex tasks.

These are the basic steps involved in executing a command in Minishell.

Resources

Lexer && Parser:
-> https://www.youtube.com/watch?v=bxpc9Pp5pZM&ab_channel=Computerphile
-> https://www.cs.purdue.edu/homes/grr/SystemsProgrammingBook/Book/Chapter5-WritingYourOwnShell.pdf
-> https://www.geeksforgeeks.org/making-linux-shell-c/

Execution:
-> https://www.youtube.com/watch?v=cex9XrZCU14&list=PLfqABt5AS4FkW5mOn2Tn9ZZLLDwA3kZUY&ab_channel=CodeVault
-> https://www.youtube.com/watch?v=Wtd-8OiZOjk&ab_channel=Jess-Inspired
-> https://www.geeksforgeeks.org/pipe-system-call/

About

Minishell is a small-scale implementation of a Unix-like shell🐚📚✨.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages