story behind shell
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 ?
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
- Command Parsing
- Command Search && Execution
- Input/Output Redirection
- Pipe Execution
- Variable Expansion
- Word
- Special characters (“, ‘, <, >, <<, >>, $, |, spc)
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).
Simple command execution steps example:
+------------+
|--| Open all |----|
| | heredocs | |
| +------------+ |
+-------------------+ +--------------------------+ | | +-------------------------+ +---------------------+
| Take cmd_tab |-----| Check for redirections |--| |-----| Change stdin and stdout |---| Execute the command |
+---------+---------+ +------------+-------------+ | | +-------------------------+ +---------------------+
| +------------+ |
|--|open other |----|
|redirictions|
+------------+
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
After finding the command path, the program executes it, launching a new process to run the 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.
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/