Skip to content

A custom unix shell. The goal is to have a good understanding of how the unix system orchestrate command execution, from reading commands through batch or interactive modes to lexical parsing to tokenization to system calls and more. It also gears towards understanding of system processes, from creation to efficient process communication

Notifications You must be signed in to change notification settings

SlamChillz/simple_shell

Repository files navigation

Simple Shell

Introduction

This repository is an Alx School Project. This project involves writing a shell like sh (Bourne Shell) by Stephen Bourne , in C, using a limited number of standard library functions, and practically applying we have learnt in C during the past 3 months Here

The goal in this project was to make us understand how a shell works. To single out some items: what is the environment, the difference between functions and system calls, how to create processes using execve...

Usage

In order to run this program,

Clone This Repo

git clone https://github.com/Slamchillz/simple_shell

compile it with

gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o hsh.
You can then run it by invoking ./hsh in that same directory.

How to use it

In order to use this shell, in a terminal, first run the program:
prompt$ ./hsh
It wil then display a simple prompt and wait for commands.
($)
A command will be of the type $ command
This shell can handle two types of commands: builtins and normal program.

List of built-ins

Currently the list of built-ins I wrote is:

  • cd [directory]
    Switch to the specified directory (path).
  • env
    Displays the environment variable
  • exit [exitstatus]
    Exit from the program with exitstatus value. 0 by default.
  • setenv VARIABLE VALUE
    Sets the enviromental variable of key VARIABLE with the value of VALUE
  • unsetenv Unset an environment variable whose key is VARIABLE
  • echo [$$] or [$?] or [$PATH] Return pid and exit statue and PATH.
Command

Basicly Every Program in $PATH It Support Single Word like ls

It Support chaining commands like ls || ls && cp new.txt file.txt ; echo $OLDPWD

It Handle Path ls /tmp

it Handle Options Like ls -l

it Handle All Three Togther Like ls -l /var

it Handle Command Path Also Like /bin/ls And All The Option And Path Like /bin/ls -l /var

it Handle Comments #

Examples Command

Example 1

Username@your-regular-prompt:~$ ./hsh
($) pwd
/home/username/
($) ^D
Username@your-regular-prompt:~$

Example 2

Username@your-regular-prompt:~$ ./hsh
($) ls -l /tmp 
-rw------- 1 username username    0 Dec  5 12:09 config-err-aAMZrR
drwx------ 3 root   root   4096 Dec  5 12:09 systemd-private-062a0eca7f2a44349733e78cb4abdff4-colord.service-V7DUzr
drwx------ 3 root   root   4096 Dec  5 12:09 systemd-private-062a0eca7f2a44349733e78cb4abdff4-rtkit-daemon.service-ANGvoV
drwx------ 3 root   root   4096 Dec  5 12:07 systemd-private-062a0eca7f2a44349733e78cb4abdff4-systemd-timesyncd.service-CdXUtH
-rw-rw-r-- 1 username username    0 Dec  5 12:09 unity_support_test.0
($) ^D
Username@your-regular-prompt:~$

Exmples Builtin

case env and exit

Username@your-regular-prompt:~$ ./hsh
USER=julien
LANGUAGE=en_US
SESSION=ubuntu
COMPIZ_CONFIG_PROFILE=ubuntu
SHLVL=1
HOME=/home/julien
C_IS=Fun_:)
DESKTOP_SESSION=ubuntu
LOGNAME=julien
TERM=xterm-256color
PATH=/home/julien/bin:/home/julien/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
DISPLAY=:0
($) exit
Username@your-regular-prompt:~$ 

Case Exit Status

Username@your-regular-prompt:~$ ./hsh
($) exit 98
Username@your-regular-prompt:~$ echo $?
98
Username@your-regular-prompt:~$

Additional Features

  • Handle Ctrl+C: your shell should not quit when the user inputs ^C
  • If no argument is given to cd the command must be interpreted like cd $HOME
  • handle the command cd -
  • Handle variables replacement
  • Handle the $? variable
  • Handle the $$ variable
  • Handle batch mode like ./hsh [filename] Where test is a file filled with command and builtin to excute.

List of functions and system calls we could use

List of allowed functions and system calls

access (man 2 access)
chdir (man 2 chdir)
close (man 2 close)
closedir (man 3 closedir)
execve (man 2 execve)
exit (man 3 exit)
fork (man 2 fork)
free (man 3 free)
fstat (man 2 fstat)
getcwd (man 3 getcwd)
getline (man 3 getline)
kill (man 2 kill)
lstat (man 2 lstat)
malloc (man 3 malloc)
open (man 2 open)
opendir (man 3 opendir)
perror (man 3 perror)
read (man 2 read)
readdir (man 3 readdir)
signal (man 2 signal)
stat (man 2 stat)
strtok (man 3 strtok)
wait (man 2 wait)
waitpid (man 2 waitpid)
wait3 (man 2 wait3)
wait4 (man 2 wait4)
write (man 2 write)
_exit (man 2 _exit)

Custom Function (Recreation of Standard Function in C)

  • _strncpy
  • _strlen
  • _putchar
  • _atoi
  • _puts
  • _strcmp
  • _isalpha
  • _reverse
  • intlen
  • _itoa
  • _join
  • _strcpy
  • _strchr
  • _strncmp
  • _strdup
  • _realloc
  • _freearray
  • _getenv
  • _readline
  • _tokenise
  • _hashtag

For More Info About It Check The Man Page by

Username@your-regular-prompt:~$ man ./man_1_simple_shell

Project Done in 15 Days

Author

About

A custom unix shell. The goal is to have a good understanding of how the unix system orchestrate command execution, from reading commands through batch or interactive modes to lexical parsing to tokenization to system calls and more. It also gears towards understanding of system processes, from creation to efficient process communication

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages