Skip to content

Shell v2 will execute directly any binary file that you want to execute in your system if it can find it directly, or is listed in the PATH environment variable.

License

Notifications You must be signed in to change notification settings

daorejuela1/shell_v2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

39 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Maintenance MIT license Awesome Badges ForTheBadge built-with-love

Shell v2

(A custom shell based on bash.)

Shell v2 is an advance command interpreter that executes commands that are read from the standard input, handles redirections (>, >>, <, <<), pipelines (|), multi-command input (cmd ; cmd2 ; cmd3), variable replacement ($?, $$, $VAR), logic operators (&&, ||) and comments (#).

Shell v2 will execute directly any binary file that you want to execute in your system if it can find it directly, or is listed in the PATH environment variable. Each input receive in interactive mode will issue a prompt and increase the line counter, if a command can not be found or execute it will return the line counter, and the respective error message immediately.

Team ๐ŸŽฎ

David Orejuela
Software Developer
David Orejuela
ย 

Motivation ๐Ÿ‹

Default Shells like Bash or Zsh are programs that take care of a lot of cases, customization and input parsing, re-creating from scratch one so complex is a really hard task. In order to customize even more a Shell it would be better to start from a base but my motivation is to deeply understand how a Shell works under the hood and what system calls are needed to perform every action.

Main logic ๐Ÿง 

Let's suppose we have this input: ls; ls > hello; ls | rev # hello world

  1. The command will be saved in history
  2. The parser will remove every comment from the input
  3. The parser will separate indicating status by those special characters in nodes of a linked list every command

Linked list example

According to the status flag, it will go to a different operator in the operator function.

Status -> Right redirection

  1. Store a backup of STODUT_FILENO
  2. Create a new file indicated by the redirection
  3. Duplicate STDOUT_FILENO into the new file FD
  4. Execute the command
  5. Restore STDOUT_FILENO

*Double right redirection is the same but the file is opened on append mode.

Status -> Left redirection

  1. Store a backup of STDIN_FILENO
  2. Read the filename on the right part of the redirection
  3. Duplicate STDIN_FILENO into the new file FD
  4. Execute the command
  5. Restore STDIN_FILENO

Status -> HEREDOC

  1. Store a backup of STDIN_FILENO
  2. Create a temp file /tmp/shell_v2heredoc_daor1475
  3. While the user input is different from the HEREDOC delimiter ask for input
  4. Duplicate STDIN_FILENO into the new file FD
  5. Execute the command
  6. Restore STDIN_FILENO

Status -> Pipeline

  1. Create the pipe
  2. Backup STDOUT_FILENO
  3. Duplicate STDOUT_FILENO into the writing-end of the pipe
  4. close writing-end of the pipe
  5. Execute the command
  6. Restore STDOUT_FILENO
  7. Backup STDIN_FILENO
  8. Duplicate STDIN_FILENO into the reading-end of the pipe
  9. close reading-end of the pipe
  10. Execute the command to the right of the pipe
  11. Restore STDIN_FILENO

*In case of multiple pipelines two pipes are used to follow the same logic.

Code style ๐Ÿ‘“

Betty โ˜ž Betty

Demo๐Ÿ“ท

Simple Commands


Pipelines


Comments


Variable expansion


Redirection


Aliases

Tech used ๐Ÿ› 

Software architecture

This C program is created under GCC version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.4) with the use of the follow system calls:

  • access
  • chdir
  • close
  • execve
  • fork
  • stat
  • open
  • read
  • wait
  • write
  • dup
  • dup2
  • pipe
  • getpid
  • __errno_location (errno macros)
  • closedir
  • exit
  • free
  • getcwd
  • getline
  • malloc
  • perror
  • strtok
  • isatty
  • printf
  • fflush
  • fprintf

Requirements ๐Ÿ“š

  • Ubuntu 14.04+
  • GCC version 4.8.4

Installation & Init ๐Ÿ“–

Please make sure that you have installed the essentials before cloning:

sudo apt-get install build-essential
  1. Clone the repository: https://github.com/daorejuela1/shell_v2/
  2. Go to the folder: cd shell_v2
  3. Compile the application: make

Usage ๐Ÿ’ช

After successfully executing the make command

Execute ./hsh to start the shell in interactive mode.

If you want to execute the shell in not interactive mode you can do it this ways:

  1. ./hsh [FILENAME]

  2. echo [COMMAND] | ./hsh

  3. cat [FILENAME] | ./hsh


To read the man page use: man ./man_1_hsh

Features ๐Ÿ“œ

Built-in commands

This shell contains builtins commands to perform certain action which means that instead of using binary files, the same shell is in charge of performing the actions:

Name Description Example
alias Define or display aliases. alias [name=value] ... [name2=value2]
cd Change the shell working directory cd [path]
env prints all the current environment variables no arguments are needed. env
exit Exits the shell with a status of N. If N is omitted, the exit status is of the last command. exit [n]
setenv Sets a new environmental variable. setenv [name] [value]
unsetenv Unsets a environmental variable. unsetenv [name]
help Display help about the specific command help [builtin command]
history Prints the commands used in the session history

Files

  • ~/.hshrc Startup file that's executed before starting the shell v2

  • ~/.hsh_history File that stores the executed commands

Contributing ๐Ÿง

Contributions are always welcome!

Please read the contribution guidelines first.

Related projects ๐Ÿ’ผ

Here are some awesome projects I have been working on:

Mastermind Hackday Project Daily tweet Monty bytecode decoder Serpent Algorithm Custom Shell v1
Monty project Tweet daily Monty Serpent Shell

Licensing ๐Ÿ”‘

License

Released in 2021 by @daorejuela1

Credits โœˆ

Special thanks to Alexandre Gautier for his guidance in the development of this project.

About

Shell v2 will execute directly any binary file that you want to execute in your system if it can find it directly, or is listed in the PATH environment variable.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published