Skip to content

Dachacho/shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SHELL

Intro

This project marks the start of my journey through low/system level development. I was always intrigued how software works on the lowest level you can get (i know this isn't all that lowest) so i've decided to start buildling projects that get close to hardware. So this marks the start.

features

  • runs all your standard system programs like ls, cat, grep, whatever you got
  • has some built-in commands:
    • exit to get out
    • cd to move around directories
    • jobs to see whats running in the background
    • fg to bring a background job to the front
    • bg to resume something you paused
    • history to see what you've been up to
  • throw an & at the end of a command to run in the background
  • you can run up to 10 jobs at once
  • command history works so you can scroll through your old commands (with up and down arrows THANKS GNU)
  • tab completion is there to save your fingers some typing (thanks GNU readline)

ONCE more lets repeat THANKS GNU readline.

Basics

This project is a basic shell. nothing fancy its not going to replace any shells you use (unless you like bad software). It just uses your systems programs : ls, cat, grep, etc. It does have few built in functions such as: exit, cd, jobs, fg, bg, history. This shell does have history and tab complete thanks to GNU readline library which does all the heavy lifting.

Implementation

Implementation is very simple (i have done nothing crazy just followed how every other shell is implemented) basically we just run a while loop until exit is inputed. In the while loop we get input buffer with readline then we tokenize this buffer and check if the command is out builtin command or systems program. If it is our built-in function we execute then function, but if its a systems program we create a new process (fork) and replace out current process (execvp). THAT'S IT.

I have also implemented a very naive way of job control which just uses array to store jobs (max jobs - 10). So we track if the jobs is ran in the background with &. if it is ran we use non blocking wait (WNOHANG). if it isn't we use normal wait. fg checks if there is a running job in the background and brings it to the main process (so we can stop or suspend it). bg checks if any job was suspended and runs that job in background.

history is done with GNU readline library. I just have an array to remember the commands and if history command is used display it.

Running the program

Readline

install readline if you don't have it (check for yourself)

macos: brew install readline

linux (ubuntu): sudo apt-get update

sudo apt-get libreadline-dev


compile it with : gcc shell.c jobs.c history.c -o shell -lreadline here lreadline flag should be used to get access to it. run the program: ./shell

(thats it. i don't know builds and make files so i think it could've been more streamlined)

Key Takeaways

This has beeen a really fun project to get to know more about OS concepts (like fork and execvp).

It really feels good when you just get that first version up and running and you see your shell doing ls or cat.

I had very hard time figuring out how to pipe and redirect (and doing those together was just a headache).

Simple/naive version that i have implemented is very easy to implement. I hacked this solution together in around 5 days.

I think everyone should atleast attempt this once to get to know processes better. or just to have fun.


this was just a fun, quick project. probably very error prone (i don't even have proper error handling done). BUT IT FUCKING WORKS.

About

Simple shell

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages