This is an implementation of a Linux shell written in C language.
- Clone this directory and
cdinto it. - Run the command
make. - Run
./shellto get a prompt of the formusername@system_name:path. - Run any command in the shell. It can entail as many number of tabs and spaces, the shell accounts for those.
- In order to exit, run
quit.
These commands have been defined by me and are contained within the shell itself.
-
pwd- Displays the name of the working directory.
- Implemented in pwd.c
-
ls [-l -a] [directory]- Lists all the files and directories in the specified directory in alphabetical order.
- Variations such as
ls, ls . , ls ..also work. - Also handles multiple directories as arguments. eg.
ls -l dir1 dir2 dir3. - Throws error if you try to
lson anything except a directory. - Also highlights directories in blue and executable files in green.
- Implemented in ls.c
-
cd [file]- Changes directory to the directory specified, throws an error if the directory does not exist.
- Implemented in cd.c
-
echo [arguments]- Displays whatever is specified in [arguments].
- Accounts for double quotes as well.
- Implemented in echo.c
-
quit- Exits the shell with return status as success, and adds updates history.txt.
-
setenv var[value]&unset var- Creates an enviornmental variable
varif it doesn't already exist and assigns it the value given unset vardestroys that environmental variable
- Creates an enviornmental variable
-
jobs- Prints a list of all currently running jobs along with their pid in order of their creation
- Gives the state of the job – Running, Sleeping, Stopped or Defunct
- Implemented in jobs.c
-
kjob <jobNumber> <signalNumber>- Takes the job id of a running job and sends a signal value to that process
- Implemented in kjob.c
-
fg <jobNumber>- Brings a running or a stopped background job with given job number to foreground.
- Implemented in fg.c
-
bg <jobNumber>- Changes a stopped background job to a running background job.
- Implemented in bg.c
-
overkill- Kills all background process at once.
- Implemented in overkill.c
- All other commands are treated as system commands like emacs, vim etc.
- To run a process in the background, follow the command with a '&' symbol. Eg.
emacs &. - Upon termination of a background process, the shell prints its PID and exit status.
- Handles
&no matter where it is in the end. eg.emacs& , emacs &, ls -l&. - Also returns number of background processes running currently.
- Implemented in run.c
-
pinfo [PID]- Prints numerous details about the process such as its status, memory, and executable path.
- Just
pinfowith no arguments gives details of the shell. - Implemented in pinfo.c
-
history [num]- Lists the last [num] commands. If no arguments are specified, it displays the last 10 commands.
- Retains the
historyeven upon Shell exit - uses history.txt. - Implemented in history.c
-
nightswatch -n [seconds] [dirty/interrupt]interruptargument prints the number of times CPU has been interrupted by keyboard.dirtyargument prints size of the dirty part of memory.- Executes every
nnumber of seconds as specified by user. - Exits when symbol
qis pressed. - Error handling done for incorrect arguments.
-
cronjob -c [command] -t [time_period] -p [duration]- Runs the command every t seconds until p time gets over.
- Cron runs in the background so you can continue using the shell whilst the command is executed periodically
-
CTRL-Z- Changes the status of currently running job to stop, and pushes it to the background.
-
CTRL-C- Sends SIGINT signal to the current foreground job of the shell.
- If there is no foreground job, then the signal does not have any effect.
-
Command Recall using ‘UP’ arrow key
- ‘UP’ key with the ‘ENTER’ key causes a new prompt to be displayed with the previous command and then that command is executed. (similar to normal Ubuntu environment).
- Pressing ‘UP’ key ‘K’ times consecutively leads to the Kth previous command getting executed.
-
Input-Output Redirection & Piping
- Handles
<,>,>>, and|operators appropriately, wherever they are in the command - Throws error if syntax is incorrect
- Handles
The code is completely modular with different .c files for each command, and a main.c binding them all together. headers.h entails all the necessary header files, and global variables.
This project is licensed under the MIT License - see the LICENSE.md file for details.