This is the README file for the implementation of "mysh" (My Simple Shell), a shell program written in C for Linux environments. It provides a user interface to create and manage processes, run system programs, and supports various features such as redirects, pipes, background execution, signal management, wild characters, and aliases. Bash commands and syntax are valid for this shell! Be careful as some complex syntaxes are not being supported.
Kanellakis Konstantinos
For a quick setup using Docker, run the following command:
$ docker run -it kanellaman/mysh-app:1.1
$ make
To run the shell, execute the following command in the terminal:
$ ./mysh
Once the shell is launched, you will see the prompt:
in-mysh-now:>
You can then enter commands and interact with the shell.
Run a program and save the output to a file:
in-mysh-now:> myProgram < file1 > file2
myProgram
can be any bash command
in-mysh-now:> myProgram<file1>file2
You can use pipes to combine commands and perform operations on their outputs.
Example
in-mysh-now:> cat file1 file2 file3 | sort > file4
This command uses the output of cat command (files file1, file2, and file3) as input to the sort command, and the result is written to file4.
You can run commands in the background using the & operator.
Example:
in-mysh-now:> sort file1 &; ls &;
Commands executed in the background run simultaneously with the next command. Background commands are not affected by signals Ctrl+C and Ctrl+Z.
You can use wild characters to specify a subset of files in the current directory.
Example:
in-mysh-now:> ls file*.t?t
This command lists the files in the current directory that have a filename starting with "file", an extension that starts and ends with the character "t", and any one character in between.
You can create and destroy aliases for commands.
Example:
- Create an alias:
in-mysh-now:> createalias myhome "cd /home/users/smith"
This creates an alias called "myhome" that is equivalent to running the command cd /home/users/smith.
-Destroy an alias:
in-mysh-now:> destroyalias myhome
This deletes the alias "myhome".
Alias of an alias is supported but only one word can be an alias!
The shell can handle simple signals.
-Ctrl+C: Sending a SIGINT signal to the running process via the mysh prompt. All foreground processes are terminated but not the mysh shell.
-Ctrl+Z: Sending a SIGSTP signal to the running process via the mysh prompt. All foreground processes are stopped but not the mysh shell.
The shell remembers the last 20 user commands and allows easy recall of the previously-run commands. Example:
- This prints out the history of commands so far.
in-mysh-now:> myhistory
- This runs the 4th command of the history if exists
in-mysh-now:> myhistory 4
Examples:
in-mysh-now:> sort<file1>file2
in-mysh-now:> cat file1 file2|sort>file2
Other examples to run...
in-mysh-now:> ls ; cd ; ls
in-mysh-now:> myhistory 3 > file1
in-mysh-now:> createalias hd "ls -l"
in-mysh-now:> hd
in-mysh-now:> cd ; history 3 ; hd
in-mysh-now:> cat file1 ; sort file2 & sleep 10
- The main function initializes variables and sets up signal handling using the
signals
function. - It enters a loop to continuously read user input until the user chooses to exit or Ctrl+D is given.
- Inside the loop, it checks for any completed
background process
using waitpid and prints a message for each completed process. - The input string is tokenized into individual commands using the tokenize function.
- The commands are separated into a two-dimensional array of tokens using the separate function.
- The code handles
aliases
andmyhistory
commands, replacing them with their respective commands. - Special commands like
exit
andcd
are handled separately. - For each command in the two-dimensional array, it sets up
input
andoutput
redirection
if necessary, forks a child process, and executes the command usingexecvp
. - Parent process handles
foreground
andbackground
execution, waits for foreground processes to complete, and updates foregroundprocess group
. - The loop continues until the user chooses to exit.