(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.
David Orejuela Software Developer |
---|
ย |
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.
Let's suppose we have this input: ls; ls > hello; ls | rev # hello world
- The command will be saved in history
- The parser will remove every comment from the input
- The parser will separate indicating status by those special characters in nodes of a linked list every command
According to the status flag, it will go to a different operator in the operator function.
- Store a backup of STODUT_FILENO
- Create a new file indicated by the redirection
- Duplicate STDOUT_FILENO into the new file FD
- Execute the command
- Restore STDOUT_FILENO
*Double right redirection is the same but the file is opened on append mode.
- Store a backup of STDIN_FILENO
- Read the
filename
on the right part of the redirection - Duplicate STDIN_FILENO into the new file FD
- Execute the command
- Restore STDIN_FILENO
- Store a backup of STDIN_FILENO
- Create a temp file
/tmp/shell_v2heredoc_daor1475
- While the user input is different from the HEREDOC delimiter ask for input
- Duplicate STDIN_FILENO into the new file FD
- Execute the command
- Restore STDIN_FILENO
- Create the pipe
- Backup STDOUT_FILENO
- Duplicate STDOUT_FILENO into the writing-end of the pipe
- close writing-end of the pipe
- Execute the command
- Restore STDOUT_FILENO
- Backup STDIN_FILENO
- Duplicate STDIN_FILENO into the reading-end of the pipe
- close reading-end of the pipe
- Execute the command to the right of the pipe
- Restore STDIN_FILENO
*In case of multiple pipelines two pipes are used to follow the same logic.
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
- Ubuntu 14.04+
- GCC version 4.8.4
Please make sure that you have installed the essentials before cloning:
sudo apt-get install build-essential
- Clone the repository:
https://github.com/daorejuela1/shell_v2/
- Go to the folder:
cd shell_v2
- Compile the application:
make
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:
-
./hsh [FILENAME]
-
echo [COMMAND] | ./hsh
-
cat [FILENAME] | ./hsh
To read the man page use: man ./man_1_hsh
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 |
-
~/.hshrc Startup file that's executed before starting the shell v2
-
~/.hsh_history File that stores the executed commands
Contributions are always welcome!
Please read the contribution guidelines first.
Here are some awesome projects I have been working on:
Mastermind Hackday Project | Daily tweet | Monty bytecode decoder | Serpent Algorithm | Custom Shell v1 |
---|---|---|---|---|
Released in 2021 by @daorejuela1
Special thanks to Alexandre Gautier for his guidance in the development of this project.